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

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

適用対象: SharePoint Foundation 2010

SharePoint Online で使用可能

メソッドまたはプロパティの値および値オブジェクトと異なり、メソッドまたはプロパティでクライアント オブジェクトを返すときは、そのオブジェクトを同じクエリ内の別のメソッドやプロパティの呼び出しのパラメーターとして使用できます。クライアント オブジェクトの詳細については、「クライアント オブジェクト、値オブジェクト、およびスカラー プロパティ」を参照してください。

次の例では、1 つのクエリの中でいくつかのクライアント オブジェクトを使用して、特定のリスト アイテムを返しています。プロパティ値を操作するときの典型例とは異なり、この例では ExecuteQuery() または ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) (JavaScript: executeQueryAsync(succeededCallback, failedCallback)) に対する 1 つのメソッド呼び出しを行うだけで済みます。

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class GetListItem
    {
        static void Main()
        {
            ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
            Web oWebsite = clientContext.Web;
            List oList = oWebsite.Lists.GetByTitle("Announcements");
            ListItem oItem = oList.GetItemById(1);

            clientContext.Load(oItem);

            clientContext.ExecuteQuery();

            Console.WriteLine(oItem["Title"]);
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
   Class GetListItem
      
      Shared Sub Main()
         Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection")
         Dim oWebsite As Web = clientContext.Web
         Dim oList As List = oWebsite.Lists.GetByTitle("Announcements")
         Dim oItem As ListItem = oList.GetItemById(1)
         
         clientContext.Load(oItem)
         
         clientContext.ExecuteQuery()
         
         Console.WriteLine(oItem("Title"))
      End Sub
   End Class
End Namespace
function getListItem() {

    var clientContext = new SP.ClientContext('/sites/TestWebs/TestWeb1');
    var oWebsite = clientContext.get_web();
    var oList = oWebsite.get_lists().getByTitle('Announcements');
    this.oListItem = oList.getItemById(1);

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

function onQuerySucceeded() {
    alert(oListItem.get_item('Title'));
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

Microsoft SharePoint Foundation 2010 は、オブジェクトのパスを使用して、オブジェクトがどのように作成されたかを追跡します。このリスト アイテムを返すために、この例では、いくつかのメンバーを使用することで生成されるオブジェクト パスを使用しています。これらは、クライアント コンテキストの Web プロパティ (JavaScript: web)、Web サイトの Lists プロパティ (JavaScript: lists)、リスト コレクションの GetByTitle(String) メソッド (JavaScript: getByTitle(strListName))、リストの GetItemById(String) メソッド (JavaScript: getItemById(id)) です。クライアント オブジェクトの作成に使用されたオブジェクト パスをオブジェクト モデルが追跡するので、同じクエリ内で他のメソッドを呼び出すためのパラメーターとしてオブジェクトを使用することもできます。

関連項目

概念

データ取得の概要

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

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

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

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