Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharePoint get ContentType Name

I am trying to get All Folders and Files from a SharePoint library, executing a single Request.

CamlQuery query = new CamlQuery();
query.ViewXml = "<View Scope='RecursiveAll' />";
var libraryName = "Specific Documents";

ListItemCollection itemsRaw = clientContext.Web.Lists.GetByTitle(libraryName).GetItems(query);
clientContext.Load(itemsRaw);
clientContext.ExecuteQuery();

This code works well, and as result I have a list of All Folders and Files within the specified library.

It seems that the files details are loaded in a lazy manner. Only the first level from details hierarchy. But I don't know how, the FieldValues collection is filled with Data.

ListItem details

I see that the ListItem ContentType.Name is not initialized.

ContentType Name is not initialized

Is it possible somehow to update the query in a manner which will load the data for ContentType in this single call.

Or the only possibility is to iterate through all files and do a load of ContentType for the specific file?

I did this in the following way:

foreach(var listItem in listItemCollection)
{
    context.Load(listItem, k => k.ContentType);
    context.ExecuteQuery();
    var contentTypeName = listItem.ContentType.Name;
}

But I am going to get this information in a single call, If it is possible, without iterating in the collection and starting multiple calls to ClientContext.

P.S.: I am new to SharePoint programming. I just want to fix a bug.

Thanks!

like image 294
mihai Avatar asked Sep 29 '22 18:09

mihai


1 Answers

As you correctly noticed in SharePoint Client Side Object Model (CSOM) ClientRuntimeContext.Load Method does not retrieve all the properties for client object.

ClientRuntimeContext.Load Method has the following syntax:

public void Load<T>(
    T clientObject,
    params Expression<Func<T, Object>>[] retrievals
)
where T : ClientObject        

where retrievals parameter is intended for specifying properties that have to be retrieved.

Secondly, since SharePoint CSOM supports Request Batching, your example could be modified to this one:

foreach (var item in items)
{
    ctx.Load(item, i => i.ContentType);
}
ctx.ExecuteQuery();

Note: request is submitted to the server only once in this example

But still the provided example requires two requests to the server:

  • retrieve list items
  • retrieve content type for list items

and it could be improved from performance perspective by reducing requests to the server till one.

Final example

The example demonstrates how to retrieve list items and specify explicitly which properties to retrieve:

 var listTitle = "Documents";
 var query = new CamlQuery();
 query.ViewXml = "<View Scope='RecursiveAll' />";

 var items = ctx.Web.Lists.GetByTitle(listTitle).GetItems(query);
 ctx.Load(items,icol => icol.Include(
                i => i.ContentType,
                i => i.FieldValues));
 ctx.ExecuteQuery();
like image 50
Vadim Gremyachev Avatar answered Oct 02 '22 14:10

Vadim Gremyachev