可以在查询的各个方法之间使用客户端对象

上次修改时间: 2010年2月9日

适用范围: SharePoint Foundation 2010

在 SharePoint Online 中提供

与方法或属性值及值对象不同,当您通过方法或属性返回客户端对象时,可以将该对象用作同一查询中其他方法或属性调用的参数。有关客户端对象的详细信息,请参阅客户端对象、值对象和标量属性

以下示例涉及在一个查询中使用多个客户端对象来返回特定的列表项。与使用属性值时通常发生的情况不同,该示例只需要调用一次 ExecuteQuery()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 会跟踪如何使用对象路径创建对象。为了返回列表项,该示例采用了因使用多个成员而产生的对象路径:客户端上下文的 Web 属性 (JavaScript: web)、网站的 Lists 属性 (JavaScript: lists)、列表集合的 GetByTitle(String) 方法 (JavaScript: getByTitle(strListName)) 以及列表的 GetItemById(String) 方法 (JavaScript: getItemById(id))。由于对象模型会跟踪创建客户端对象时所用的对象路径,因此代码可以继续使用该对象作为参数,来调用同一查询中的其他方法。

请参阅

概念

数据检索概述

在访问值属性之前调用 Load 和 ExecuteQuery

值对象不能在查询中跨方法使用

对相同对象的数据检索分组

检索客户端对象并不会检索所有属性