简体   繁体   中英

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详细信息

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

ContentType Name未初始化

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 .

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

Thanks!

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();

The final example does not work (in SP2010).

There is an exception "The query expression is not supported" If you explicitly states all required fields then the solution below works.

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

var items = ctx.Web.Lists.GetByTitle(listTitle).GetItems(query);

string[] fieldsToMigrate = new string[] { "Title", "FieldA", "FieldB" };
ctx.Load(items, a => a.Include(b => b.ContentType, b => b["FileRef"]));
foreach (var f in fieldsToLoad) {
    ctx.Load(items, includes => includes.Include(a => a[f]));
}

ctx.ExecuteQuery();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM