5

I'm using the SharePoint 2010 Managed Client Object Model to retrieve a list of listitems:

      string formattedDate = theDate.ToString("yyyy-MM-ddTHH:mm:ssZ");
            CamlQuery absenceQuery = new CamlQuery();
            absenceQuery.ViewXml = "<View><Query>" +
                "<Where><Eq><FieldRef Name='AbsenceDate'/><Value Type='DateTime' IncludeTimeValue='FALSE'>" + formattedDate + "</Value></Eq></Where>"
                + "</Query>"
                  +"<ViewFields>"
   + "  <FieldRef Name='Employee' /><FieldRef Name='AbsenceDate' /><FieldRef Name='AbsenceTime' /><FieldRef Name='ContentType' />"
   + "</ViewFields>"
                +"<RowLimit>2000</RowLimit></View>";
            ListItemCollection absences = clientContext.Web.Lists.GetByTitle("Absences").GetItems(absenceQuery);
    clientContext.Load(absences);

I need to include the ContentType in the results so I tried changing that last line to

 clientContext.Load(absences,
                items=> items.Include(
                     item=>item.ContentType,
                     item=>item.ContentType.Name
                ));

The ContentType Name was not included in the results. When I tried to access it throws PropertOrFieldNotInitializedException.

I found a link here http://msdn.microsoft.com/en-us/library/office/ee534956(v=office.14).aspx that says Because this example uses the Include(IQueryable, []) method, only the specified properties are available after query execution. Therefore, you receive a PropertyOrFieldNotInitializedException if you attempt to access other properties beyond those that have been specified. In addition, you receive this error if you attempt to use properties such as ContentType or ParentList to access the properties of containing objects.

Anybody know how to retrieve the Items with the contentType name?

6

In order to retrieve ContentType property for ListItem it should be specified as demonstrated below:

clientContext.Load(listItems,items=> items.Include(item=>item.ContentType));

but in your case the list of fields that should be retrieved are also explicitly specified via Query using ViewFields element:

<ViewFields>...</ViewFields>

Solution

In addition to

clientContext.Load(listItems,items=> items.Include(item=>item.ContentType));

specify also FieldRef element for ContentTypeId in query:

<View><Query></Query><ViewFields><FieldRef Name='ContentTypeId' /></ViewFields></View>
  • To get ContentType field, you must include into ViewFields ContentTypeId (Worked also in SPOM). – dbardakov May 7 '14 at 7:21
  • Added both ContentType and ContentTypeId into ViewFields in the CAML query, added items.Include(i=>i.ContentType) before Load and ExecuteQuery but I am still receiving ListItems without ContentTypes, any idea what I could be doing wrong? – Michal Hainc Aug 9 '18 at 11:14

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.