次の方法で共有


コード スニペット: 外部コンテンツ タイプの 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 つの外部コンテンツ タイプ。

この例を使用するには

  1. Visual Studio を開始し、C# コンソール アプリケーション プロジェクトを作成します。プロジェクトを作成するときに、[.NET Framework 3.5] を選択します。

  2. [表示] メニューから、[プロパティ ページ] をクリックしてプロジェクト プロパティを表示します。

  3. [ビルド] タブから、[プラットフォーム ターゲット] で、[Any 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()