实现 SharePoint Foundation REST 接口

上次修改时间: 2010年12月3日

适用范围: SharePoint Foundation 2010

这一部分演练介绍如何创建自定义 Windows Communication Foundation (WCF) Web 服务,说明了如何在 Microsoft Visual Studio 2010 中创建一个 Windows 窗体应用程序,该应用程序使用 ADO.NET Data Services 和 Microsoft SharePoint Foundation 代表性状态传输 (REST) 接口来通过两个 DataGridView 控件与列表数据进行交互。SharePoint Foundation REST 接口提供通过这两个控件与 Projects 列表和 Employees 列表进行交互的数据上下文。Employees 控件显示与用户在 Projects 控件中已选择的项目项关联的员工。此外,Projects 控件允许用户对 Projects 数据源中的项进行更改,并使用这些更改更新 SharePoint 列表数据。

创建 Windows 窗体应用程序以与列表数据进行交互

  1. 打开 Visual Studio,单击"文件",指向"新建",然后单击"项目"。在"新建项目"对话框中,选择"已安装的模板"框中的"Visual Basic"或"Visual C#",然后依次选择"Windows"和"Windows 窗体应用程序",在"名称"框中键入"ProjectTracker"。单击"确定"。

  2. 若要连接到存储在 SharePoint 网站中的数据,请依次单击"数据"和"添加新数据源"。在"数据源配置向导"中选择"SharePoint",然后单击"下一步"。

  3. 在"添加服务引用"框中键入到 ListData.svc 的路径,ListData.svc 是一个提供对 SharePoint Foundation REST 接口的访问的服务。此路径必须包括包含列表的网站,并且必须指定 _vti_bin 虚拟目录,例如 http:// Server/sites/SiteCollection/WebSite/_vti_bin/ListData.svc。若要添加对项目的服务引用,请依次单击"转到"、"确定"和"完成"。此服务引用会将应用程序连接到 REST 接口,并检索 SharePoint Foundation 网站的实体数据模型以便定义实体类。SharePoint Foundation 列表数据将作为强类型数据源附加到此服务引用。

  4. 在 Form1.cs 的"设计"视图中,依次单击"数据"和"显示数据源"以打开"数据源"框。为了提供与列表数据交互的 UI,请将 Projects 列表和 Employees 列表的 DataGridView 控件拖放到"Form1"上。

  5. 若要在每个 DataGridView 控件中删除不需要显示的列,请右键单击相应的控件,单击"编辑列",然后在"编辑列"框中,选择"选定的列"框中的每一列,并单击"删除"。从 Projects DataGridView 控件中删除除"Title"(标题)、"Description"(说明)和"Due Date"(截止日期)之外的所有列。从 Employees DataGridView 控件中删除除"Full Name"(全名)、"Job Title"(职务)和"Team"(团队)之外的所有列。在"绑定列属性"框中,将 Projects 列表的"Title"(标题)列和 Employees 列表的"Full Name"(全名)列设置为"填充",然后单击"确定"。

  6. 若要使用 SharePoint Foundation 网站的列表数据填充 DataGridView 控件,请双击 Form1 的边缘以查看其代码。添加一个 using 语句以导入您创建的 ServiceReference1 命名空间,并使用一个构造函数来创建强类型的数据上下文以表示列表数据。可以使用此数据上下文将请求发送到网站。在此构造函数中,指定提供用于访问指定网站中的列表数据的 REST 接口的 WCF 服务的 URI,如下所示。

    Imports ProjectTracker.ServiceReference1
    Imports System.Net
    
    Public Class Form1
    
        Private Shared websiteUrl As String = "http://YourServer/sites/YourSiteCollection/YourSite"
    
        Private context As New MyWebsiteDataContext(
            New Uri(websiteUrl + "/_vti_bin/listdata.svc"))
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ProjectTracker
    {
        using ServiceReference1;
        using System.Net;    
    
        public partial class Form1 : Form
        {
    
        private static string websiteUrl= "http://YourServer/sites/YourSiteCollection/YourSite";
    
            MyWebsiteDataContext context = new MyWebsiteDataContext(
                new Uri(websiteUrl + "/_vti_bin/listdata.svc"));
    
            public Form1()
            {
                InitializeComponent();
            }
    
  7. 在 Form1_Load 事件中,设置针对请求的身份验证。添加一个 using 语句以导入 System.Net 命名空间。Windows 集成的身份验证允许您使用凭据缓存的默认凭据。此外,在 Form1_Load 事件中,使用数据上下文类对由 Visual Studio 为 Projects DataGridView 控件生成的数据源进行填充,如下所示。

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            context.Credentials = CredentialCache.DefaultCredentials
    
            ProjectsBindingSource.DataSource = context.Projects
        End Sub
    
        private void Form1_Load(object sender, EventArgs e)
        {
            context.Credentials = CredentialCache.DefaultCredentials;
            projectsBindingSource.DataSource = context.Projects;
        }
    
  8. 若要使用列表数据填充 Employees DataGridView 控件,则在 Projects 数据源中为 CurrentChanged 事件创建一个处理程序。当用户在 Projects DataGridView 中选择一个项目时,将使用根据所选项目筛选的员工列表填充 Employees DataGridView。若要在设计视图中添加 CurrentChanged 事件,可以右键单击窗体下的"projectsBindingSource",单击"属性",再单击"属性"窗口中的事件图标,然后双击"CurrentChanged"事件。

  9. 使用语言集成查询 (LINQ) 查询来筛选其项目标识符 (ID) 等于当前选定项目的 ID 的员工。您可以通过使用项目数据源的 Current 属性并将当前项强制转换为"ProjectsItem"类型来获取当前选定的项目,如下所示。

        Private Sub ProjectsBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectsBindingSource.CurrentChanged
            EmployeesBindingSource.DataSource =
                From emp In context.Employees _
                Where emp.Project.Id = DirectCast(ProjectsBindingSource.Current, ProjectsItem).Id _
                Select emp
        End Sub
    
        private void projectsBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            employeesBindingSource.DataSource =
                from emp in context.Employees
                where emp.Project.Id == ((ProjectsItem)projectsBindingSource.Current).Id
                select emp;
        }
    

    请注意,此示例使用从 Employees 列表中的 Project 查找列映射的 Projects 列表的 Project 导航属性。

  10. 若要添加更新支持,以便在用户修改窗体中的数据时网站中的数据也会相应地进行修改,请通过右键单击 Form1 菜单中的"保存"图标并单击"已启用",启用设计视图中的"保存"按钮。双击"保存"可查看代码并插入"projectsBindingNavigatorSaveItem_Click"事件处理程序。添加以下代码行以指示 ADO.NET Data Services 将更改保存回网站。

        Private Sub ProjectsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectsBindingNavigatorSaveItem.Click
            context.SaveChanges()
        End Sub
    
        private void projectsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            context.SaveChanges();
        }
    
  11. 由于您创建的是 Windows 窗体应用程序,因此必须添加指示有关已更改的对象的数据上下文的代码。若要添加 CurrentItemChanged 事件,请右键单击"设计"视图中的"projectBindingSource",单击"属性",再单击"属性"窗口中的事件图标,然后双击"CurrentItemChanged"事件以在 Form1.cs 中插入处理程序。在此处理程序中使用 ADO.NET 数据上下文的 UpdateObject(Object) 方法,并使用 Current 属性指示哪个属性已更改,如下所示。

        Private Sub ProjectsBindingSource_CurrentItemChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectsBindingSource.CurrentItemChanged
            context.UpdateObject(ProjectsBindingSource.Current)
        End Sub
    End Class
    
        private void projectsBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            context.UpdateObject(projectsBindingSource.Current);
        }
    }}
    
  12. 若要测试窗体,请按 F5,并对 Projects DataGridView 中的某个项进行一些更改。单击"保存"按钮时,您所做的更改将会传播到网站上。

    备注

    在单击"保存"按钮之前,可能需要在所更改的单元格外部单击,以便查看更改是否生效。

有关完整的 Form1 代码示例,请参阅 填写 SharePoint Foundation WCF 表单 1 示例

请参阅

概念

实现 SharePoint Foundation REST 接口

使用 ADO.NET 数据服务查询 SharePoint Foundation

其他资源

WCF REST Programming Model