0

Short description of my problem

I'm requesting certain properties from SharePoint via CSOM but when accessing them I get the dreaded PropertyOrFieldNotInitializedException. But looking at the transmitted data using Fiddler I can see the requested data has been transmitted.

More details about each step

Complete code of repro console application:

static void Main(string[] args)
{
    var authMgr = new AuthenticationManager();
    using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant("https://contoso.sharepoint.com/sites/dev", "admin@contoso.onmicrosoft.com", "tree desk wall magma core"))
    {
        var web = ctx.Web;
        ctx.Load(web, w => w.RoleAssignments);
        ctx.LoadQuery(web.RoleAssignments.Include(b => b.Member.Title));
        ctx.ExecuteQuery();
    }
}

I want to retrieve web.RoleAssignments[n].Member.Title for all RoleAssignments. Executing this I can see in Fiddler that the title is included in the response:

enter image description here

But when I try to access the Title in the Immediate window of Visual Studio I get the PropertyOrFieldNotInitializedException:

web.RoleAssignments[0].Member.Title
'web.RoleAssignments[0].Member.Title' threw an exception of type 'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException'
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146233079
    HelpLink: null
    InnerException: null
    Message: "The property or field 'Title' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested."
    Source: "Microsoft.SharePoint.Client.Runtime"
    StackTrace: "   at Microsoft.SharePoint.Client.ClientObject.CheckUninitializedProperty(String propName)\r\n   at Microsoft.SharePoint.Client.Principal.get_Title()"
    TargetSite: {Void CheckUninitializedProperty(System.String)}

It's like the CSOM library fails to propagate the Title information it received into the Member property. Is this a restriction of CSOM? But why transmit the information in the first place? Can somebody explain this?

1

I was doing it wrong. It seems like CSOM does not aggregate data of related queries. (This was my expectation.)

The corrected code the get the Title property is:

var web = ctx.Web;
ctx.Load(web.RoleAssignments, a => a.Include(b => b.Member.Title));
ctx.ExecuteQuery();

Now web.RoleAssignments[n].Member.Title succeeds.

Doh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.