创建基本 SharePoint Foundation 客户端应用程序
上次修改时间: 2010年8月3日
适用范围: SharePoint Foundation 2010
若要创建使用客户端对象模型的 .NET 托管客户端应用程序,您必须设置对两个客户端库 DLL 的引用:Microsoft.SharePoint.Client.dll 和 Microsoft.SharePoint.Client.Runtime.dll。可以从运行 Microsoft SharePoint Foundation 2010 的服务器的 %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\ISAPI 文件夹将这两个 DLL 复制到要在其中创建应用程序的远程计算机上。
创建基本控制台应用程序
以下编程任务逐步演示了在 Microsoft Visual Studio 中创建基本控制台应用程序的过程,该应用程序使用 Microsoft.SharePoint.Client 命名空间中的对象来返回网站属性。
在"文件"菜单上,指向"新建",然后单击"项目"。
在"新建项目"对话框中,在"已安装的模板"面板中选择"Visual Basic"或"Visual C#"。选择"Windows",然后选择"控制台应用程序"。从中央面板顶部的下拉列表中选择".NET Framework 3.5"。在"名称"和"位置"框中,键入该项目的名称和位置。然后单击"确定"。
在 Windows 资源管理器中,将两个 DLL 从服务器的 %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\ISAPI 文件夹复制到控制台应用程序的顶级文件夹中。
在"解决方案资源管理器"中右键单击该项目,单击"添加引用",然后在"添加引用"对话框中,单击"浏览"选项卡并导航到向其中复制了 Microsoft.SharePoint.Client.dll 和 Microsoft.SharePoint.Client.Runtime.dll 的文件夹。选择这两个 DLL,然后单击"确定"。
以下示例返回并显示位于 ClientContext() 构造函数中指定的 URL 处的网站的标题。Load<T>(T, []) 方法指定要从服务器检索的对象,在本例中为网站;ExecuteQuery() 执行查询。由于已加载网站对象,因此其所有默认属性都可在控制台中显示。有关在加载对象或集合时默认情况下不可用的属性的信息,请参阅数据检索概述。
using System; using Microsoft.SharePoint.Client; namespace Microsoft.SDK.SharePointServices.Samples { class DisplayWebTitle { static void Main() { ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection/MyWebSite"); Web oWebsite = clientContext.Web; clientContext.Load(oWebsite); clientContext.ExecuteQuery(); Console.WriteLine("Title: {0} Created: {1}", oWebsite.Title, oWebsite.Created); } } }
Imports System Imports Microsoft.SharePoint.Client Namespace Microsoft.SDK.SharePointServices.Samples Class DisplayWebTitle Public Overloads Shared Sub Main() Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection/MyWebSite") Dim oWebsite As Web = clientContext.Web clientContext.Load(oWebsite) clientContext.ExecuteQuery() Console.WriteLine("Title: {0} Created: {1}", oWebsite.Title, oWebsite.Created) End Sub End Class End Namespace
按 F5 即可运行该应用程序并在控制台中显示网站的标题和创建日期。