代码段:在服务器上的外部列表中创建项
上次修改时间: 2010年9月27日
适用范围: SharePoint Server 2010
本文内容
说明
先决条件
使用此示例
说明
使用 SPList 类应用的 Add 方法在外部列表中创建项。下面的代码段演示如何向外部列表添加项。
先决条件
Microsoft SharePoint Server 2010 或 Microsoft SharePoint Foundation 2010 位于服务器上。
Microsoft Visual Studio。
至少有一个外部内容类型在 BDC 元数据存储区中注册,并且有一个外部列表基于此外部内容类型。
备注
此示例中使用的外部列表不能使用 Passthrough 身份验证。
使用此示例
启动 Visual Studio 并创建一个新的 C# 控制台应用程序项目。创建项目时选择".NET Framework 3.5"。
从"视图"菜单中,单击"属性页"以显示项目属性。
在"生成"选项卡中,为"目标平台"选择"任何 CPU"。
关闭项目属性窗口。
在"解决方案资源管理器"中的"引用"下,删除 System 和 System.Core 之外的所有项目引用。
向项目中添加以下引用:
Microsoft.SharePoint
System.Web
用此过程结尾处列出的代码替换 Program.cs 中自动生成的代码。
将 <siteUrl> 和 <ExternalListName> 字符串值以及 <Field>, <Value> 对替换为有效值。
保存该项目。
编译并运行该项目。
using System;
using System.Web;
using Microsoft.SharePoint;
namespace Microsoft.SDK.SharePoint.Samples.Bdc.CreateEntity
{
class Program
{
static void Main(string[] args)
{
using(SPSite site = new SPSite("<siteUrl>"))
{
SPServiceContext context = SPServiceContext.GetContext(site);
SPServiceContextScope contextScope = new SPServiceContextScope(context);
SPWeb web = site.OpenWeb();
SPList list = web.Lists["<ExternalListName>"];
SPListItem item = list.Items.Add();
item["<Field1>"] = "<Value1>";
item["<Field2>"] = "<Value2>";
// Set all fields.
item.Update();
}
}
}
}