クライアント側オブジェクト モデルを実装する

最終更新日: 2011年2月7日

適用対象: SharePoint Foundation 2010

このチュートリアルの前半の「SharePoint Foundation REST インターフェイスを実装する」では、ADO.NET Data Services および SharePoint Foundation REST インターフェイスを使用して、リストおよびライブラリに格納されているデータを操作する方法について説明しています。ただし、REST インターフェイスでは、より高度なアプリケーションで必要な場合に、SharePoint Foundation サイトの他のデータにアクセスすることができません。前のバージョンの SharePoint Foundation では、既定の Web サービスを使用してデータにアクセスし、ラッパー クラスを定義して、返された SOAP XML をアプリケーションのオブジェクトに変換していました。Microsoft SharePoint Foundation 2010 には、マネージ クライアント側オブジェクト モデルが導入され、オブジェクト階層内の Web サイト レベルまたはその下位のレベルで、新しい REST インターフェイスよりも幅広い SharePoint Foundation オブジェクトにアクセスできます。また、このオブジェクト モデルにより、Web サービスを使用するよりも簡単に、リモート アプリケーションでデータを操作できるようになりました。

この例では、「SharePoint Foundation REST インターフェイスを実装する」で説明した Windows Forms アプリケーションが作成されていることを前提としています。この例では、クライアント オブジェクト モデルを使用してフォームのタイトル バーを Web サイトのタイトルに設定し、さらに、ユーザーがフォームの [Star Project] ボタンをクリックしたときに、選択されているプロジェクト アイテムに基づいて、サイトの Projects リストの説明を変更します。

クライアント オブジェクト モデルを使用してフォームのタイトルおよびリストの説明を変更する

  1. 2 つのクライアント モデル アセンブリ、Microsoft.SharePoint.Client.dll および Microsoft.SharePoint.Client.Runtime.dll への参照を追加します。ソリューション エクスプローラーで [参照] を右クリックし、[参照の追加] をクリックします。[参照の追加] ダイアログ ボックスの [.NET] タブで、[Microsoft.SharePoint.Client] および [Microsoft.SharePoint.Client.Runtime] を選択します。次に、[OK] をクリックします。

  2. Form1.cs または Form1.vb で、ステートメントを追加し、Microsoft.SharePoint.Client 名前空間をプロジェクトにインポートします。次のように、ClientContext() コンストラクターを使用して Web サイトに接続し、クライアント コンテキストを使用してクライアント オブジェクト モデルにアクセスします。

    Imports ProjectTracker.ServiceReference1
    Imports System.Net
    Imports Microsoft.SharePoint.Client
    
    Public Class Form1
    
        Private Shared websiteUrl As String = "http://YourServer/sites/YourSiteCollection/YourWebSite"
    
        Private context As New TestWebsDataContext(
            New Uri(websiteUrl  + "/_vti_bin/listdata.svc"))
    
        Private clientContext As New ClientContext(websiteUrl)
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.SharePoint.Client;
    
    namespace ProjectTracker
    {
        using ServiceReference1;
        using System.Net;
    
        public partial class Form1 : Form
        {
    
            private static string websiteUrl= "http://YourServer/sites/YourSiteCollection/YourWebSite";
    
            TestWebsDataContext context = new TestWebsDataContext(
                new Uri(websiteUrl + "/_vti_bin/listdata.svc"));
    
            ClientContext clientContext = new ClientContext(websiteUrl);
    
            public Form1()
            {
                InitializeComponent();
            }
    
  3. Form1_Load イベントを使用して、要求を SharePoint Foundation サーバーに送信します。既定の資格情報がクライアント オブジェクト モデルによって自動的に使用されます。オブジェクトを使用するには、Load<T>(T, []) メソッドを呼び出してオブジェクトのプロパティの読み込みを明示的に要求し、ExecuteQuery() メソッド (Microsoft Silverlight バージョンのオブジェクト モデルでは ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) メソッド) を呼び出してその要求を SharePoint Foundation に送信し、Web サイト オブジェクトにデータを設定します。次のスニペットでは、フォームのタイトル バーが Web サイトのタイトルに設定されます。

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            context.Credentials = CredentialCache.DefaultCredentials
    
            ProjectsBindingSource.DataSource = context.Projects
    
            clientContext.Load(clientContext.Web)
            clientContext.ExecuteQuery()
            Me.Text = clientContext.Web.Title
        End Sub
    
        Private Sub ProjectsBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectsBindingSource.CurrentChanged
            EmployeesBindingSource.DataSource =
                From emp In context.Employees _
                Where emp.Project.Id = DirectCast(ProjectsBindingSource.Current, ProjectsItem).Id _
                Select emp
        End Sub
    
        Private Sub ProjectsBindingSource_CurrentItemChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectsBindingSource.CurrentItemChanged
            context.UpdateObject(ProjectsBindingSource.Current)
        End Sub
    
        Private Sub ProjectsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectsBindingNavigatorSaveItem.Click
            context.SaveChanges()
        End Sub
    
            private void Form1_Load(object sender, EventArgs e)
            {
                context.Credentials = CredentialCache.DefaultCredentials;
                projectsBindingSource.DataSource = context.Projects;
    
                clientContext.Load(clientContext.Web);
                clientContext.ExecuteQuery();
    
                this.Text = clientContext.Web.Title;
    
            }
    
            private void projectsBindingSource_CurrentChanged(object sender, EventArgs e)
            {
                employeesBindingSource.DataSource =
                    from emp in context.Employees
                    where emp.Project.Id == ((ProjectsItem)projectsBindingSource.Current).Id
                    select emp;
            }
    
            private void projectsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
            {
                context.SaveChanges();
            }
    
            private void projectsBindingSource_CurrentItemChanged(object sender, EventArgs e)
            {
                context.UpdateObject(projectsBindingSource.Current);
            }
    
  4. この例では、フォームのタイトルを変更するほかに、ユーザーの選択に基づいて Web サイトに表示される Projects リストの説明も変更します。ユーザーが Projects リストでアイテムを選択してボタンをクリックすると、選択されているプロジェクトが強調表示されるようにリストの説明が変更されます。ボタンを Form1 に追加するには、[保存] ボタンの横にあるフォーム タイトル バーを右クリックし、表示されたドロップダウン リストで [ボタン] を選択します。

  5. ボタンの [プロパティ] ウィンドウで、[表示スタイル] を [テキスト] に設定し、[テキスト] 設定の値として「Star Project」と入力します。

  6. フォームのボタンをダブルクリックして Click イベントを開き、次のコードを追加します。このスニペットは、クライアント オブジェクト モデルの GetByTitle(String) メソッドを使用してリストを返し、その説明にアクセスします。この例では、ADO.NET Data Services Projects データ ソースを使用して、現在選択されているプロジェクトのタイトルを取得し、選択されているアイテムを ProjectsItem オブジェクトとしてキャストして、そのアイテムの Title プロパティにアクセスします。

        Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
            Dim oList As List = clientContext.Web.Lists.GetByTitle("Projects")
    
            oList.Description = String.Format("Star Project of the Week is {0}!!!", DirectCast(ProjectsBindingSource.Current, ProjectsItem).Title)
    
            oList.Update()
            clientContext.ExecuteQuery()
        End Sub
    End Class
    
            private void toolStripButton1_Click(object sender, EventArgs e)
            {
                List oList = clientContext.Web.Lists.GetByTitle("Projects");
    
                oList.Description = string.Format("Star Project of the Week is {0}!!!",
                    ((ProjectsItem)projectsBindingSource.Current).Title);
    
                oList.Update();
                clientContext.ExecuteQuery();
            }
    }}
    
  7. F5 キーを押してアプリケーションを実行し、フォームのタイトルが変更されていることを確認します。プロジェクトの DataGridView コントロールでアイテムの 1 つを選択し、[Star Project] をクリックすると、リストの説明が変更されます。

完全な Form1 コード例については、「SharePoint Foundation の WCF Form1 サンプルを完了する」を参照してください。

関連項目

概念

[チュートリアル] SharePoint Foundation でカスタム WCF サービスの作成と実装を行う

[方法] リストを取得する

[方法] リストを作成、更新、または削除する

その他の技術情報

マネージ クライアント オブジェクト モデル