Получение групповых данных из одного объекта

Дата последнего изменения: 27 мая 2011 г.

Применимо к: SharePoint Foundation 2010

Доступно на сайте SharePoint Online

В целях повышения производительности всегда следует пытаться группировать вместе запросы на извлечение данных в одном объекте.

В следующем примере оба метода Method1 и Method2 извлекают заголовок и описание веб-сайта, а также описание списка "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 код для извлечения заголовка и для описания веб-сайта сгруппирован вместе, а в методе Method2 код для извлечения тех же свойств разделен по разным действиям. Method2 вызывает два разных запроса в один и тот же объект веб-сайта, и таким образом для одного объекта появляется два набора результатов. Поскольку клиентская библиотека стремится возвращать последовательные данные, второй набор результатов будет включать как заголовок, так и описание веб-сайта, хотя первый набор результатов уже содержит заголовок веб-сайта.

Различие между этими двумя методами иллюстрируется различием между следующими командами 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 перед доступом к свойствам значения

Объекты значения не могут быть использованы в методах запроса

Клиентские объекты можно использовать в методах запроса

При получении клиентского объекта получаются не все свойства