共用方式為


Retrieving a SharePoint Client Object Does Not Retrieve All Properties

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

In the server object model, when you return an SPWeb object, all of its properties become available for use in code. But to improve performance in the client object model, when you return a Web object (JavaScript: Web) certain properties are not included, and you must explicitly retrieve them. For example, the Web class (JavaScript: Web) inherits the HasUniqueRoleAssignments property (JavaScript: hasUniqueRoleAssignments) from the SecurableObject class (JavaScript: SecurableObject), but when you return a Web object (JavaScript: Web) this property is not retrieved by default. For more information about properties that are not returned by default when you retrieve an object, see Data Retrieval Overview.

The following example, which aims to display the title of a Web site and whether the Web site has unique role assignments, returns a PropertyOrFieldNotInitializedException on the second call to write to the console because the HasUniqueRoleAssignments property (JavaScript: hasUniqueRoleAssignments) has not been explicitly retrieved.

Incorrect Code

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);
        }
    }
}

The following revision of the previous example explicitly retrieves the HasUniqueRoleAssignments property (JavaScript: hasUniqueRoleAssignments), as well as the Title property (JavaScript: title).

Correct Code

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());
}

See Also

Concepts

Data Retrieval Overview

How to: Retrieve List Items

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

Group Data Retrieval on the Same SharePoint Client Object