SharePoint Client Objects Can Be Used Across Methods in a Query

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

Unlike method or property values and value objects, when you return a client object through a method or property, you can use the object as a parameter for another method or property call in the same query. For more information about client objects, see Client Objects, Value Objects, and Scalar Properties.

The following example involves using several client objects in one query to return a specific list item. Unlike what typically occurs when working with property values, the example only needs to make one method call to ExecuteQuery() or ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) (JavaScript: executeQueryAsync(succeededCallback, failedCallback)).

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 keeps track of how objects are created by using object paths. To return the list item, the example uses the object path that results from using several members: the Web property (JavaScript: web) of the client context, the Lists property (JavaScript: lists) of the Web site, the GetByTitle(String) method (JavaScript: getByTitle(strListName)) of the list collection, and then the GetItemById(String) method (JavaScript: getItemById(id)) of the list. Because the object model keeps track of the object path that is used to create the client object, code could proceed to use the object as a parameter for invoking other methods within the same query.

See Also

Concepts

Data Retrieval Overview

Call Load and ExecuteQuery Before Accessing Value Properties

Value Objects Cannot Be Used Across Methods in a Query

Group Data Retrieval on the Same SharePoint Client Object

Retrieving a SharePoint Client Object Does Not Retrieve All Properties