代码段:执行外部内容类型的 Finder 方法实例
上次修改时间: 2010年5月6日
适用范围: SharePoint Server 2010
本文内容
说明
先决条件
使用此示例
说明
以下代码示例演示如何在服务器上通过使用 BDC 运行时对象模型来以编程方式执行某个外部内容类型 Finder 方法实例。
先决条件
Microsoft SharePoint Server 2010 或 Microsoft SharePoint Foundation 2010 位于服务器上。
Microsoft Visual Studio 位于客户端上。
至少在 BDC 元数据存储中注册了一个外部内容类型。
使用此示例
启动 Visual Studio 并创建一个 C# 控制台应用程序项目。在创建此项目时,请选择".NET Framework 3.5"。
从"视图"菜单中,单击"属性页"以显示项目属性。
在"生成"选项卡中,为"目标平台"选择"任何 CPU"。
在"解决方案资源管理器"中的"引用"下,删除 System 和 System.Core 之外的所有项目引用。
将以下引用添加到项目中:
Microsoft.BusinessData
Microsoft.SharePoint
System.Web
使用此过程结束时列出的代码替换 Program.cs 中的代码。
将 <siteUrl>、<nameSpace> 和 <entityName> 的值替换为有效值。
保存项目。
编译并运行项目。
using System;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.SharePoint.Administration;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.MetadataModel.Collections;
namespace SDKSamples
{
class Methods
{
static void Main(string[] args)
{
BDCGetAllFieldsAndRecords();
}
// Get the fields and read data from an external content type.
public static void BDCGetAllFieldsAndRecords()
{
// Specify the SiteURL, Namespace, and the Entity Name.
string SiteURL = "<siteUrl>";
string nameSpace = "<nameSpace>";
string entityName = "<entityName>";
using (SPSite site = new SPSite(SiteURL))
{
using (new Microsoft.SharePoint.SPServiceContextScope(
SPServiceContext.GetContext(site)))
{
BdcService service =
SPFarm.Local.Services.GetValue<BdcService>(
String.Empty);
IMetadataCatalog catalog =
service.GetDatabaseBackedMetadataCatalog(
SPServiceContext.Current);
IEntity entity = catalog.GetEntity(
nameSpace, entityName);
ILobSystemInstance LobSysteminstance =
entity.GetLobSystem().
GetLobSystemInstances()[0].Value;
// Display the fields in the Entity.
IFieldCollection fieldCollection =
entity.GetFinderView("Read List").Fields;
foreach (IField field in fieldCollection)
{
Console.Write(field.Name.PadRight(20));
}
Console.WriteLine();
// Display all the records in the Entity.
IMethodInstance methodInstance = entity.GetMethodInstance(
"Read List", MethodInstanceType.Finder);
IEntityInstanceEnumerator ientityInstanceEnumerator =
entity.FindFiltered(
methodInstance.GetFilters(), LobSysteminstance);
while (ientityInstanceEnumerator.MoveNext())
{
foreach (IField field in fieldCollection)
{
Console.Write(
ientityInstanceEnumerator.
Current[field.Name].ToString().PadRight(20));
}
Console.WriteLine();
}
}
Console.ReadKey();
}
}
}
}
请参阅
引用
GetDatabaseBackedMetadataCatalog(SPServiceContext)
GetMethodInstance(String, MethodInstanceType)