Group Data Retrieval on the Same SharePoint Client Object

To improve performance, always try to group data retrieval requests together on the same SharePoint 2010 client object.

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

In the following example of grouping data retrieval requests, Method1 and Method2 both retrieve the title and description of a Web site and the description of the Announcements list, but the performance of Method1 surpasses the performance of 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));
}

In Method1, the code to retrieve the Web site title and description is grouped together, while in Method2, the code to retrieve the same properties is separated by another action. Method2 triggers two separate queries on the same Web site object and therefore there are two result sets for the same object. Because the client library aims to return consistent data, the second result set includes both the title and description of the Web site, while the first result set also includes the Web site title.

The difference between the previous methods is illustrated by the difference between the following SQL commands.

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 ...

See Also

Concepts

Data Retrieval Overview

Call Load and ExecuteQuery Before Accessing Value Properties

Value Objects Cannot Be Used Across Methods in a Query

SharePoint Client Objects Can Be Used Across Methods in a Query

Retrieving a SharePoint Client Object Does Not Retrieve All Properties