コード スニペット: クライアントで外部リストのアイテムを作成する

最終更新日: 2010年9月27日

適用対象: SharePoint Server 2010

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

説明

List クラスの AddItem メソッドを使用して、クライアントの外部リストにアイテムを作成できます。以下のコード スニペットは、クライアント オブジェクト モデルを使用して外部リストにアイテムを追加する方法を示します。

前提条件

  • サーバー上の Microsoft SharePoint Server 2010 あるいは Microsoft SharePoint Foundation 2010。

  • サーバー上の少なくとも 1 つの外部リスト。

  • クライアント コンピューター上の Microsoft Office Professional Plus 2010 と Microsoft .NET Framework 3.5。

  • Microsoft Visual Studio。

この例を使用するには

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

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

  3. [ビルド] タブから、[プラットフォーム ターゲット] で、[Any CPU] を選択します。

  4. プロジェクト プロパティ ウィンドウを閉じます。

  5. [ソリューション エクスプローラー] の [参照設定] で、[System] と [System.Core] を除いて、すべてのプロジェクト参照を削除します。

  6. プロジェクトに以下の参照を追加します。

    1. Microsoft.SharePoint.Client

    2. Microsoft.SharePoint.Client.Runtime

  7. この手順の最後に示すコードで、Program.cs で自動生成されたコードを置換します。

  8. <TargetSiteUrl>、<TargetListName>、および MyField/MyValue ペアの値を有効な値で置換します。

  9. プロジェクトを保存します。

  10. プロジェクトをコンパイルして、実行します。

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.Sharepoint.Samples
{
    class Program
    {
        // Note: Replace these with your actual Site URL and List name.
        private static string TargetSiteUrl = "<TargetSiteUrl>";
        private static string TargetListName = "<TargetListName>";

        /// <summary>
        /// Example to show using CSOM to retrieve external List data.        
        /// </summary>        
        static void Main(string[] args)
        {
            ClientContext clientContext = new ClientContext(TargetSiteUrl);
            List externalList = clientContext.Web.Lists.GetByTitle(
                TargetListName);
            ListItem newItem = externalList.AddItem(
                new ListItemCreationInformation());
            newItem["MyField"] = "MyValue";
            // Set all required fields.
            newItem.Update();
            clientContext.ExecuteQuery();
        }
    }
}