代码段:执行外部内容类型的 Creator 方法实例

上次修改时间: 2010年5月6日

适用范围: SharePoint Server 2010

本文内容
说明
先决条件
使用此示例

说明

以下代码示例说明如何通过使用服务器上的 BDC 运行时对象模型,以编程方式执行外部内容类型的 Creator 方法实例。

先决条件

  • Microsoft SharePoint Server 2010 或 Microsoft SharePoint Foundation 2010 位于服务器上。

  • Microsoft .NET Framework 3.5 位于客户端计算机上。

  • Microsoft Visual Studio。

  • BDC 元数据存储中至少已注册一个外部内容类型。

使用此示例

  1. 启动 Visual Studio 并创建一个 C# 控制台应用程序项目。在创建此项目时,请选择".NET Framework 3.5"。

  2. 从"视图"菜单中,单击"属性页"以显示项目属性。

  3. 在"生成"选项卡中,为"目标平台"选择"任何 CPU"。

  4. 关闭项目属性窗口。

  5. 在"解决方案资源管理器"中的"引用"下,删除 System 和 System.Core 之外的所有项目引用。

  6. 将以下引用添加到项目中:

    1. Microsoft.BusinessData

    2. Microsoft.SharePoint

    3. System.Web

  7. 将 Program.cs 中自动生成的代码替换为此过程结束时列出的代码。

  8. 将 <SiteUrl>、<nameSpace> 和 <entityName> 的值替换为有效值。

  9. 保存项目。

  10. 编译并运行项目。

using System;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.MetadataModel.Collections;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;

namespace SDKSamples
{
    class Methods
    {
        static void Main(string[] args)
        {
            BDCCreate();
        }

        //How To: Create an External Content Type
        public static void BDCCreate()
        {
            //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;

                    IView createView = entity.GetCreatorView("Create");
                    IFieldValueDictionary fieldValueDictionary =
                        createView.GetDefaultValues();

                    // The following will work only for Sales.Customer table in AdventureWorks2008

                    //Enter number of Customers To Create
                    Console.Write("\nEnter number of Customers To Create : ");
                    int number = int.Parse(Console.ReadLine());

                    // Code below can be used to edit the values and create Records.
                    for (int i = 1; i <= number; i++)
                    {
                        // Example values for TerritoryID : 1,2,4
                        Console.Write("\nEnter the TerritoryID : ");
                        fieldValueDictionary["TerritoryID"] = int.Parse(Console.ReadLine());
                        // Example value CustomerType : S
                        Console.Write("\nEnter the CustomerType : ");
                        fieldValueDictionary["CustomerType"] = Console.ReadLine();
                        fieldValueDictionary["rowguid"] = Guid.NewGuid();
                        fieldValueDictionary["ModifiedDate"] = DateTime.Now;
                        Identity id = entity.Create(fieldValueDictionary, LobSysteminstance);
                        Console.WriteLine("Successfully Inserted Item Number: {0}", i);
                    }
                }
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }
        }
    }
}

请参阅

引用

BdcService

Services

IMetadataCatalog

GetDatabaseBackedMetadataCatalog(SPServiceContext)

GetEntity(String, String)

IEntity

GetLobSystem()

GetLobSystemInstances()

ILobSystemInstance

IView

GetCreatorView(String)

GetDefaultValues()