次の方法で共有


コード スニペット: 外部コンテンツ タイプの Updater メソッド インスタンスを実行する

最終更新日: 2010年5月6日

適用対象: SharePoint Server 2010

この記事の内容
説明
前提条件
この例を使用するには

説明

以下のコード例は、サーバー上で BDC ランタイム オブジェクト モデルを使用することにより、プログラミングによって外部コンテンツ タイプの Updater メソッド インスタンスを実行する方法を示します。

前提条件

  • サーバー上の 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;
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)
        {
            BDCUpdate();
        }

        //How To: Edit an item from an External Content Type
        public static void BDCUpdate()
        {
            //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 edit : ");
                    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 old values
                        Console.WriteLine("\nOld Values : ");

                        foreach (IField field in fieldCollection)
                        {
                            Console.WriteLine(
                                field.Name.PadRight(20) + ":" +
                                ientityinstance[field.Name].ToString());
                        }

                        // The following will work only for Sales.Customer table in AdventureWorks2008
                        // Changing value of "TerritoryID" field
                        string fieldName = "TerritoryID";
                        
                        // Example Value for TerritoryID will be 1,4,6 etc.
                        Console.Write(
                           "\nEnter the new value for the column {0}: ",
                           fieldName);
                        ientityinstance[fieldName] = int.Parse(Console.ReadLine());
                        ientityinstance["ModifiedDate"] = DateTime.Now;

                        ientityinstance.Update();
                        Console.WriteLine("Record updated");
                        
                        //Display the Updated values
                        Console.WriteLine("\nUpdated Values : ");

                        foreach (IField field in fieldCollection)
                        {
                            Console.WriteLine(
                                field.Name.PadRight(20) + ":" +
                                ientityinstance[field.Name].ToString());
                        }

                    }
                    catch (ObjectNotFoundException exception)
                    {
                        Console.WriteLine(
                            "Identity column with value {0} not found...",
                            identityColumnValue);
                    }

                }
            }
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }
}

関連項目

参照

BdcService

Services

IMetadataCatalog

GetDatabaseBackedMetadataCatalog(SPServiceContext)

GetEntity(String, String)

IEntity

GetLobSystem()

GetLobSystemInstances()

ILobSystemInstance

Identity

IEntityInstance

FindSpecific(Identity, String, ILobSystemInstance)

GetFinderView(String)

IView

Fields

IFieldCollection

Update()