上次修改时间: 2010年5月6日
适用范围: SharePoint Server 2010
本文内容
说明
先决条件
使用此示例
说明
下面的代码示例演示如何使用服务器上的 BDC 运行时对象模型,以编程方式执行外部内容类型的 Deleter 方法实例。
先决条件
Microsoft SharePoint Server 2010 或 Microsoft SharePoint Foundation 2010 位于服务器上。
Microsoft .NET Framework 3.5 位于客户端计算机上。
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.SharePoint;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.MetadataModel.Collections;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint.Administration;
namespace SDKSamples
{
    class Methods
    {
        static void Main(string[] args)
        {
            BDCDelete();
        }
        //How To: Edit an item from an External Content Type
        public static void BDCDelete()
        {
            //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;
                    // Accept the user input for identity value
                    Console.Write(
                        "\nEnter identity value for which you want to delete : ");
                    int identityColumnValue =
                        int.Parse(Console.ReadLine());
                    Identity identity =
                        new Identity(identityColumnValue);
                    try
                    {
                        IEntityInstance ientityinstance =
                            entity.FindSpecific(
                            identity, "Read Item", LobSysteminstance);
                        IFieldCollection fieldCollection =
                            entity.GetFinderView("Read List").Fields;
                        //Display the item
                        foreach (IField field in fieldCollection)
                        {
                            Console.WriteLine(
                                field.Name.PadRight(20) + ":" +
                                ientityinstance[field.Name].ToString());
                        }
                        Console.Write("\nDo you want to delete this record (Y/N)?");
                        string confirmDelete = Console.ReadLine();
                        if (string.Compare(confirmDelete, "Y", true) == 0)
                        {
                            ientityinstance.Delete();
                            Console.WriteLine("Record deleted");
                        }
                    }
                    catch (ObjectNotFoundException exception)
                    {
                        Console.WriteLine(
                            "Identity column with value {0} not found...",
                            identityColumnValue);
                    }
                }
            }
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }
}
请参阅
引用
GetDatabaseBackedMetadataCatalog(SPServiceContext)