代码段:在客户端上的外部列表中创建项
上次修改时间: 2010年9月27日
适用范围: SharePoint Server 2010
本文内容
说明
必备组件
使用此示例
说明
使用 List 类的 AddItem 方法从客户端在外部列表中创建项。下面的代码段演示如何使用客户端对象模型向外部列表中添加项。
必备组件
服务器上安装了 Microsoft SharePoint Server 2010 或 Microsoft SharePoint Foundation 2010。
服务器上至少有一个外部列表。
客户端计算机上安装了 Microsoft Office Professional Plus 2010 和 Microsoft .NET Framework 3.5。
Microsoft Visual Studio。
使用此示例
在客户端计算机上启动 Visual Studio,然后创建 C# 控制台应用程序项目。在创建项目时,选择".NET Framework 3.5"。
从"视图"菜单中,单击"属性页"以显示项目属性。
在"生成"选项卡中,为"目标平台"选择"任何 CPU"。
关闭项目属性窗口。
在"解决方案资源管理器"中的"引用"下,移除 System 和 System.Core 之外的所有项目引用。
将以下引用添加到项目中:
Microsoft.SharePoint.Client
Microsoft.SharePoint.Client.Runtime
用此过程末尾列出的代码替换 Program.cs 中自动生成的代码。
使用有效值替换 <TargetSiteUrl>、<TargetListName> 和 MyField/MyValue 对的值。
保存该项目。
编译并运行该项目。
using System;
using Microsoft.SharePoint.Client;
namespace Microsoft.SDK.Sharepoint.Samples
{
class Program
{
// Note: Replace these with your actual Site URL and List name.
private static string TargetSiteUrl = "<TargetSiteUrl>";
private static string TargetListName = "<TargetListName>";
/// <summary>
/// Example to show using CSOM to retrieve external List data.
/// </summary>
static void Main(string[] args)
{
ClientContext clientContext = new ClientContext(TargetSiteUrl);
List externalList = clientContext.Web.Lists.GetByTitle(
TargetListName);
ListItem newItem = externalList.AddItem(
new ListItemCreationInformation());
newItem["MyField"] = "MyValue";
// Set all required fields.
newItem.Update();
clientContext.ExecuteQuery();
}
}
}