实现客户端对象模型

上次修改时间: 2011年2月7日

适用范围: SharePoint Foundation 2010

本演练前面部分实现 SharePoint Foundation REST 接口显示如何使用 ADO.NET Data Services 和 SharePoint Foundation REST 接口处理存储在列表和库中的数据。但 REST 接口不提供对 SharePoint Foundation 网站中其他数据的访问,而在更高级的应用程序中可能需要访问这些数据。在以前版本的 SharePoint Foundation 中,您可能曾经使用默认的 Web 服务访问数据,然后定义包装类并将返回的 SOAP XML 转换为您的应用程序的对象。Microsoft SharePoint Foundation 2010 引入了一个托管客户端对象模型,与新 REST 接口相比,该模型在对象层次结构内的网站级别或更低级别上提供对更广泛 SharePoint Foundation 对象集的访问,从而使在远程应用程序中处理数据比使用 Web 服务更容易。

该示例假定您已创建实现 SharePoint Foundation REST 接口中所述的 Windows 窗体应用程序。该示例使用客户端对象模型将窗体的标题栏中的文本设置为网站的标题,并根据用户在窗体中单击"启动项目"时选择的项目项来更改网站上"项目"列表的描述。

使用客户端对象模型更改窗体标题和列表描述

  1. 在两个客户端对象模型程序集 Microsoft.SharePoint.Client.dll 和 Microsoft.SharePoint.Client.Runtime.dll 之间添加一个引用。在"解决方案资源管理器"中单击"引用",再单击"添加引用"。在"添加引用"对话框的".NET"选项卡上,选择 Microsoft.SharePoint.Client 和 Microsoft.SharePoint.Client.Runtime,然后单击"确定"。

  2. 在 Form1.cs 或 Form1.vb 中,添加语句以将 Microsoft.SharePoint.Client 命名空间导入项目。使用 ClientContext() 构造函数连接至网站,以使用客户端上下文获取客户端对象模型的入口,如下所示。

    Imports ProjectTracker.ServiceReference1
    Imports System.Net
    Imports Microsoft.SharePoint.Client
    
    Public Class Form1
    
        Private Shared websiteUrl As String = "http://YourServer/sites/YourSiteCollection/YourWebSite"
    
        Private context As New TestWebsDataContext(
            New Uri(websiteUrl  + "/_vti_bin/listdata.svc"))
    
        Private clientContext As New ClientContext(websiteUrl)
    
        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;
    using Microsoft.SharePoint.Client;
    
    namespace ProjectTracker
    {
        using ServiceReference1;
        using System.Net;
    
        public partial class Form1 : Form
        {
    
            private static string websiteUrl= "http://YourServer/sites/YourSiteCollection/YourWebSite";
    
            TestWebsDataContext context = new TestWebsDataContext(
                new Uri(websiteUrl + "/_vti_bin/listdata.svc"));
    
            ClientContext clientContext = new ClientContext(websiteUrl);
    
            public Form1()
            {
                InitializeComponent();
            }
    
  3. 使用 Form1_Load 事件将请求发送到 SharePoint Foundation 服务器。客户端对象模型自动使用默认凭据。若要使用对象,您必须调用 Load<T>(T, []) 方法以明确请求加载该对象的属性,然后调用 ExecuteQuery() 方法(也可以是 Microsoft Silverlight 版本的对象模型中的 ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) 方法),以将请求发送到 SharePoint Foundation 并使用数据填充网站对象。在以下代码段中,窗体的标题栏设置为网站的标题。

        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
    
            clientContext.Load(clientContext.Web)
            clientContext.ExecuteQuery()
            Me.Text = clientContext.Web.Title
        End Sub
    
        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 Sub ProjectsBindingSource_CurrentItemChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectsBindingSource.CurrentItemChanged
            context.UpdateObject(ProjectsBindingSource.Current)
        End Sub
    
        Private Sub ProjectsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectsBindingNavigatorSaveItem.Click
            context.SaveChanges()
        End Sub
    
            private void Form1_Load(object sender, EventArgs e)
            {
                context.Credentials = CredentialCache.DefaultCredentials;
                projectsBindingSource.DataSource = context.Projects;
    
                clientContext.Load(clientContext.Web);
                clientContext.ExecuteQuery();
    
                this.Text = clientContext.Web.Title;
    
            }
    
            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;
            }
    
            private void projectsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
            {
                context.SaveChanges();
            }
    
            private void projectsBindingSource_CurrentItemChanged(object sender, EventArgs e)
            {
                context.UpdateObject(projectsBindingSource.Current);
            }
    
  4. 除了更改窗体的标题,该示例还会基于用户的选择更改网站中显示的"项目"列表的描述。当用户选择"项目"列表中的某个项目并单击某个按钮时,该列表的描述将更改为突出显示选定的项目。若要为 Form1 添加按钮,请右键单击"保存"按钮旁边的窗体标题栏,并在出现的下拉列表中选择"按钮"。

  5. 在按钮的"属性"窗口中,将"DisplayStyle"设置为"文本",并键入启动项目作为该"文本"设置的值。

  6. 双击窗体中的该按钮以打开其 Click 事件,并添加以下代码,该代码段使用客户端对象模型的 GetByTitle(String) 方法返回列表并访问其描述。该示例使用 ADO.NET Data Services 项目数据源获取当前选定项目的标题,并将选定项设为 ProjectsItem 对象以访问该项的 Title 属性。

        Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
            Dim oList As List = clientContext.Web.Lists.GetByTitle("Projects")
    
            oList.Description = String.Format("Star Project of the Week is {0}!!!", DirectCast(ProjectsBindingSource.Current, ProjectsItem).Title)
    
            oList.Update()
            clientContext.ExecuteQuery()
        End Sub
    End Class
    
            private void toolStripButton1_Click(object sender, EventArgs e)
            {
                List oList = clientContext.Web.Lists.GetByTitle("Projects");
    
                oList.Description = string.Format("Star Project of the Week is {0}!!!",
                    ((ProjectsItem)projectsBindingSource.Current).Title);
    
                oList.Update();
                clientContext.ExecuteQuery();
            }
    }}
    
  7. 按 F5 运行应用程序以查看窗体的标题是否已更改。选择项目 DataGridView 控件中的其中一项,然后单击"启动项目"以更改列表的描述。

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

请参阅

概念

演练:在 SharePoint Foundation 中创建和实现自定义 WCF 服务

如何:检索列表

如何:创建、更新和删除列表

其他资源

托管客户端对象模型