値プロパティを使用する前に Load と ExecuteQuery を呼び出す

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

適用対象: SharePoint Foundation 2010

SharePoint Online で使用可能

クライアント オブジェクトの値プロパティにアクセスするには、その前に ClientContext オブジェクト (JavaScript: ClientContext) の Load<T>(T, []) メソッド (JavaScript: load(clientObject)) と、ExecuteQuery() メソッドまたは ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) メソッド (JavaScript: executeQueryAsync(succeededCallback, failedCallback)) を呼び出す必要があります。

Microsoft SharePoint Foundation 2010 サーバー オブジェクト モデルに精通している場合、サーバー ライブラリを使用するコードを作成するときと同じような方法で、クライアント ライブラリを使用するコードを作成しようとします。たとえば、Web サイトのタイトルを表示する必要がある場合、次のような C# コードを作成して、クライアント ライブラリを使用しようとする可能性があります。

正しくないコード

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class WebSiteTitle
    {
        static void Main()
        {
            ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
            Web oWebsite = clientContext.Web;
            Console.WriteLine(oWebsite.Title);
        }
    }
}

コードをコンパイルすると、PropertyOrFieldNotInitializedException が返されます。これは、プロパティの値がサーバーから取得されていないことが原因です。クライアント ライブラリは SQL のようなプログラミング パターンを使用するため、このコード例は例外を返します。SQL を使用する場合は、次の操作を行う必要があります。

  1. アドホック SQL クエリまたはストアド プロシージャーを使用して SQL クエリを作成します。

  2. SQL クエリを実行します。

  3. SQL から結果を読み込みます。

クライアント オブジェクト モデルでは、SQL と同じパターンを使用します。メソッドを呼び出すときには、クライアント ライブラリを使用してクエリを作成します。作成するクエリは、実行されるまで蓄積できます。ただし、Load<T>(T, []) メソッド (JavaScript: load(clientObject)) を通じて該当のオブジェクトまたはプロパティをまず読み込み、ExecuteQuery() メソッドまたは ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) メソッド (JavaScript: executeQueryAsync(succeededCallback, failedCallback)) を呼び出すまで、クエリはサーバーに送信されません。前のコードを動作させるには、次のように変更する必要があります。

正しいコード

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class WebSiteTitle
    {
        static void Main()
        {
            ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
            Web oWebsite = clientContext.Web;

            clientContext.Load(oWebsite,
                w=>w.Title);

            clientContext.ExecuteQuery();

            Console.WriteLine(oWebsite.Title);
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class WebSiteTitle
      
        Shared Sub Main()
            Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection")
             Dim oWebsite As Web = clientContext.Web
         
             clientContext.Load(oWebsite, Function(w) w.Title)
         
             clientContext.ExecuteQuery()
         
             Console.WriteLine(oWebsite.Title)
        End Sub
    End Class
End Namespace
function retrieveWebSite() {
    var clientContext = new SP.ClientContext('/sites/MySiteCollection');
    this.oWebsite = clientContext.get_web();

    clientContext.load(oWebsite, 'Title');

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {
    alert('Title: ' + this.oWebsite.get_title());
}
    
function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

パフォーマンスを向上するために、前の例に追加した最初のメソッド呼び出しでは、LINQ クエリ式を使用して、Web サイトの Title プロパティ (JavaScript: title) を明示的に求めています。2 番目のメソッド呼び出しで、クエリを実行します。クライアント オブジェクト モデルを通じて Web サイトのタイトルを指定することで、SQL コマンド SELECT Title FROM Webs と同様に動作します。Web サイトのすべてのプロパティを取得するには、clientContext.Load(oWebsite); を使用します。これは、SQL コマンド SELECT * FROM Webs と同様です。

関連項目

概念

データ取得の概要

クエリ内のメソッド間で値オブジェクトを使用できない

クライアント オブジェクトをクエリ内でメソッド横断的に使用できる

同じオブジェクト上のグループ データの取得

クライアント オブジェクトの取得によって全プロパティが取得されるわけではない