Export (0) Print
Expand All

Retrieving a SharePoint Client Object Does Not Retrieve All Properties

SharePoint 2010

Last modified: February 09, 2010

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);
        }
    }
}
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());
}
Show:
© 2014 Microsoft