3

I'm trying to get a particular set of columns for items in a list, as well as RoleAssignments which is a non-default property.

Assuming my CAML, where I define my ViewFields, is in a variable CAMLtemplate, I can get the desired columns:

clientContext.Load(CAMLtemplate);
clientContext.ExecuteQuery();

However when I add the additional properties, it seems I'm no longer getting the columns I've specified in my CAML:

clientContext.Load(CAMLtemplate, 
    iCol => iCol.Include(
                 item => item.RoleAssignments,
                 item => item.RoleAssignments.Include(
                                              ra => ra.Member.LoginName,
                                              ra => ra.RoleDefinitionBindings)));
clientContext.ExecuteQuery();

...I get an error when trying to work with a column I've requested in my CAML, like The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

This seems to suggest that I either need to explicitly request every column in my iCol.Include (seems crazy, there are lots of columns, and why bother with the CAML ViewFields if this is the case?), or do something else. How can I simply get the columns I want, as well as the additional property I want?

1
  • Try to use clientContext.ExecuteQuery(); just after clientContext.Load(CAMLtemplate); & then after try to apply query.
    – Hardik
    Commented May 8, 2017 at 13:50

2 Answers 2

3

It turns out that by putting my two calls to the Load method one after the other, I could retrieve the columns and then the further attributes. I still don't understand why this can't be done in a single call, but this answer gave me the clue I needed. This works:

clientContext.Load(CAMLtemplate);
clientContext.Load(CAMLtemplate, 
     iCol => iCol.Include(
                 item => item.RoleAssignments,
                 item => item.RoleAssignments.Include(
                                          ra => ra.Member.LoginName,
                                          ra => ra.RoleDefinitionBindings)));
clientContext.ExecuteQuery();
2

You are getting this error because you are using the property of the object which is not yet initialized. So to initialize this just write

clientContext.ExecuteQuery();

after the line:

clientContext.Load(CAMLtemplate);

Please see below example

using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class RetrieveListItems
    {
        static void Main()
        {
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");

            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = "<View><Query><Where><Geq><FieldRef Name='ID'/>" +
                "<Value Type='Number'>10</Value></Geq></Where></Query><RowLimit>100</RowLimit></View>";
            ListItemCollection collListItem = oList.GetItems(camlQuery);

            clientContext.Load(collListItem);

            clientContext.ExecuteQuery();

            foreach (ListItem oListItem in collListItem)
            {
                Console.WriteLine("ID: {0} \nTitle: {1} \nBody: {2}", oListItem.Id, oListItem["Title"], oListItem["Body"]);
            }
        }
    }
}

For more clarification to load fields, Please see below links.

Syntax for including fields dynamically in CSOM query?

How to include custom fields in clientContext.load()?

5
  • Hi, thanks but I have ExecuteQuery before I try to access the properties. The point of my question is about mixing CAML ViewFields with the Include for additional properties in the Load method.
    – sasfrog
    Commented May 8, 2017 at 14:02
  • 1
    For your concern, Please check the answer here..sharepoint.stackexchange.com/questions/128650/… as well as sharepoint.stackexchange.com/questions/89634/…
    – Hardik
    Commented May 8, 2017 at 14:06
  • Did you got the solution? If yes then i will put above comment in my answer. :)
    – Hardik
    Commented May 8, 2017 at 14:11
  • Aha. So I can actually just make the 2 Load calls one after each other, to cumulatively specify in Loads what I want. Maybe there's some doco I've missed, it wasn't very clear, but your second link gave me the clue. Thanks!
    – sasfrog
    Commented May 8, 2017 at 14:11
  • 1
    You can accept my answer or urs so user will find solution from it. Added both links in my answer. It might help others. :)
    – Hardik
    Commented May 8, 2017 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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