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

最終更新日: 2011年5月27日

適用対象: SharePoint Foundation 2010

SharePoint Online で使用可能

パフォーマンスを向上させるには、同じオブジェクトのデータ取得要求はできるだけまとめるようにします。

次の例では、Method1 と Method2 の両方が Web サイトのタイトルと説明、および Announcements リストの説明を取得していますが、Method1 のパフォーマンスの方が Method2 のパフォーマンスよりも優れています。

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class RetrieveData
    {
        // The following example increases performance by grouping the Title and Description 
        // property calls together in the same Load method call.
        static void Method1()
        {
            ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
            Web oWebsite = clientContext.Web;
            List oList = oWebsite.Lists.GetByTitle("Announcements");

            clientContext.Load(oWebsite, 
                website => website.Title, 
                website => website.Description);

            clientContext.Load(oList, 
                list => list.Description);

            clientContext.ExecuteQuery();
        }

        // The following example decreases performance because the Title and Description
        // property calls are not grouped together in the same Load method call.
        static void Method2()
        {
            ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
            Web oWebsite = clientContext.Web;
            List oList = oWebsite.Lists.GetByTitle("Announcements");

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

            clientContext.Load(oList, 
                list => list.Description);

            clientContext.Load(oWebsite, 
                website => website.Description);

            clientContext.ExecuteQuery();
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class RetrieveData

        // The following example increases performance by grouping the Title and Description 
        // property calls together in the same Load method call.
        Shared Sub Method1()
            Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection")
            Dim oWebsite As Web = clientContext.Web
            Dim oList As List = oWebsite.Lists.GetByTitle("Announcements")
         
            clientContext.Load(oWebsite, Function(website) website.Title, _
                Function(website) website.Description)

            clientContext.Load(oList, Function(list) list.Description)
         
            clientContext.ExecuteQuery()
        End Sub 'Method1
      
        ' The following example decreases performance because the Title and Description
        ' property calls are not grouped together in the same Load method call.      
        Shared Sub Method2()
            Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection")
            Dim oWebsite As Web = clientContext.Web
            Dim oList As List = oWebsite.Lists.GetByTitle("Announcements")
         
            clientContext.Load(oWebsite, Function(website) website.Title)

            clientContext.Load(oList, Function(list) list.Description)

            clientContext.Load(oWebsite, Function(website) website.Description)
         
            clientContext.ExecuteQuery()

        End Sub
    End Class
End Namespace
// The following example increases performance by grouping the Title and Description 
// property calls together in the same Load method call.
function Method1 () {

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

        clientContext.load(oWebsite, 'Title', 'Description');
        clientContext.load(oList, 'Description');
        
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

// The following example decreases performance because the Title and Description
// property calls are not grouped together in the same Load method call.
function Method2 () {

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

        clientContext.load(oWebsite, 'Title');
        clientContext.load(oList, 'Description');
        clientContext.load(oWebsite, 'Description');
        
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

Method1 では Web サイトのタイトルと説明を取得するコードがまとめられていますが、Method2 では、同じプロパティを取得するコードが他の操作によって切り離されています。Method2 では、同じ Web サイトで 2 つのクエリが個別にトリガーされているので、同じオブジェクトに 2 つの結果セットがあります。クライアント ライブラリは一貫したデータを返すことを目標としているため、2 番目の結果セットには Web サイトのタイトルと説明の両方が含まれています。一方、最初の結果セットにも Web サイトのタイトルが含まれています。

前のメソッド間の違いを、次の SQL コマンド間の違いによって示します。

Method1:
SELECT Title, Description FROM Webs WHERE ...
SELECT Description FROM Lists WHERE ...

Method2:
SELECT Title FROM Webs WHERE ...
SELECT Description FROM Lists WHERE ...
SELECT Title, Description FROM Webs WHERE ...

関連項目

概念

データ取得の概要

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

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

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

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