检索客户端对象并不会检索所有属性
上次修改时间: 2010年2月9日
适用范围: SharePoint Foundation 2010
在服务器对象模型中,当您返回 SPWeb 对象时,该对象的所有属性均可在代码中使用。但是为了提高客户端对象模型中的性能,当您返回 Web 对象 (JavaScript: Web) 时,将不包含某些属性,您必须显示检索这些属性。例如,Web 类 (JavaScript: Web) 会从 SecurableObject 类 (JavaScript: SecurableObject) 继承 HasUniqueRoleAssignments 属性 (JavaScript: hasUniqueRoleAssignments),但当您返回 Web 对象 (JavaScript: Web) 时,默认情况下不检索该属性。有关在检索对象时默认情况下不返回的属性的详细信息,请参阅数据检索概述。
以下示例旨在显示网站的标题以及该网站是否有唯一的角色分配,该示例会在第二次调用时返回一个 PropertyOrFieldNotInitializedException 以写入控制台,因为尚未明确检索 HasUniqueRoleAssignments 属性 (JavaScript: hasUniqueRoleAssignments)。
错误代码
using System;
using Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class RetrieveProperties
{
static void Main()
{
ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
Web oWebsite = clientContext.Web;
clientContext.Load(oWebsite);
clientContext.ExecuteQuery();
Console.WriteLine(oWebsite.Title);
Console.WriteLine(oWebsite.HasUniqueRoleAssignments);
}
}
}
上述示例的以下修订版本可明确检索 HasUniqueRoleAssignments 属性 (JavaScript: hasUniqueRoleAssignments) 和 Title 属性 (JavaScript: title)。
正确代码
using System;
using Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class RetrieveProperties
{
static void Main()
{
ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
Web oWebsite = clientContext.Web;
clientContext.Load(
oWebsite,
website => website.Title,
website => website.HasUniqueRoleAssignments);
clientContext.ExecuteQuery();
Console.WriteLine("Title: " + oWebsite.Title +
"Unique role assignments: " + oWebsite.HasUniqueRoleAssignments);
}
}
}
Imports System
Imports Microsoft.SharePoint.Client
Namespace Microsoft.SDK.SharePointServices.Samples
Class RetrieveProperties
Shared Sub Main()
Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection")
Dim oWebsite As Web = clientContext.Web
clientContext.Load(oWebsite, _
Function(website) website.Title, _
Function(website) website.HasUniqueRoleAssignments)
clientContext.ExecuteQuery()
Console.WriteLine("Title: " & oWebsite.Title & ControlChars.Lf & _
"Unique role assignments: " & oWebsite.HasUniqueRoleAssignments)
End Sub
End Class
End Namespace
function retrieveProperties () {
var clientContext = new SP.ClientContext('/sites/TestWebs/TestWeb1');
this.oWebsite = clientContext.get_web();
clientContext.load(oWebsite, 'Title', 'HasUniqueRoleAssignments');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
alert('Title: ' + oWebsite.get_title() +
'\nUnique role assignments: ' + oWebsite.get_hasUniqueRoleAssignments());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}