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

上次修改时间: 2011年5月27日

适用范围: SharePoint Foundation 2010

在 SharePoint Online 中提供

在使用方法或属性返回值,或返回从 ClientValueObject 类 (JavaScript: ClientValueObject) 继承的对象时,不能将该值用作同一查询中其他方法调用的参数。有关值对象的详细信息,请参阅客户端对象、值对象和标量属性

例如,假设您要创建一个其显示名称与网站标题相同的 SharePoint 列表。如果您熟悉 SharePoint Foundation 服务器对象模型,则可能会尝试按如下所示编写代码。

错误代码

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class CreateList
    {
        static void Main()
        {
            ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
            Web oWebsite = clientContext.Web;

            ListCreationInformation listCreationInfo = new ListCreationInformation();
            listCreationInfo.TemplateType = 104;
            listCreationInfo.Title = oWebsite.Title;
            List oList = oWebsite.Lists.Add(listCreationInfo);

            clientContext.ExecuteQuery();
        }
    }
}

编译该代码将会返回 PropertyOrFieldNotInitializedException,因为"属性或字段尚未初始化"。该示例返回异常的原因是:在调用 ExecuteQuery() 之前,网站对象的 Title 属性 (JavaScript: title) 不可用。尽管 SQL 允许您声明包含网站标题的本地变量,并允许您在创建列表时使用该本地变量,但是您不能在客户端对象模型中声明本地变量。因此,您必须调用两次 ExecuteQuery()ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) (JavaScript: executeQueryAsync(succeededCallback, failedCallback)),第一次调用返回 Title 属性 (JavaScript: title) 的值,第二次调用创建列表,如以下修改后的示例所示。

正确代码

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class CreateList
    {
        static void Main()
        {
            ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
            Web oWebsite = clientContext.Web;

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

            clientContext.ExecuteQuery();

            ListCreationInformation listCreationInfo = new ListCreationInformation();
            listCreationInfo.TemplateType = 104;
            listCreationInfo.Title = oWebsite.Title;
            List oList = oWebsite.Lists.Add(listCreationInfo);

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

Namespace Microsoft.SDK.SharePointServices.Samples
   Class CreateList
      
      Shared Sub Main()
         Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection")
         Dim oWebsite As Web = clientContext.Web
         
         clientContext.Load(oWebsite, Function(w) w.Title)
         
         clientContext.ExecuteQuery()
         
         Dim listCreationInfo As New ListCreationInformation()
         listCreationInfo.TemplateType = 104
         listCreationInfo.Title = oWebsite.Title
         Dim oList As List = oWebsite.Lists.Add(listCreationInfo)
         
         clientContext.ExecuteQuery()
      End Sub
   End Class
End Namespace
function getWebSiteTitle() {
    this.clientContext = new SP.ClientContext('/sites/TestWebs/TestWeb1');
    this.oWebsite = clientContext.get_web();

    clientContext.load(oWebsite, 'Title');

    clientContext.executeQueryAsync(Function.createDelegate(this, this.createList), Function.createDelegate(this, this.onQueryFailed));    
}

function createList() {
    var listCreationInfo = new SP.ListCreationInformation();
    listCreationInfo.set_templateType(104);
    listCreationInfo.set_title(oWebsite.get_title());

    var oList = oWebsite.get_lists().add(listCreationInfo);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {
    alert('List created.');
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

请参阅

概念

数据检索概述

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

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

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

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