5

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!

4

2 に答える 2

4

SharePoint Client Side Object Model (CSOM) ClientRuntimeContext.Load メソッドで正しく認識されているように、クライアント オブジェクトのすべてのプロパティが取得されるわけではありません。

ClientRuntimeContext.Load メソッドの構文は次のとおりです。

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

retrievalsパラメータは、取得する必要があるプロパティを指定するためのものです。

次に、SharePoint CSOM はRequest Batching をサポートしているため、例を次のように変更できます。

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

注: この例では、リクエストはサーバーに 1 回だけ送信されます。

ただし、提供されている例では、サーバーへの 2 つの要求が必要です。

  • リスト項目を取得する
  • リスト アイテムのコンテンツ タイプを取得する

サーバーへのリクエストを1つに減らすことで、パフォーマンスの観点から 改善できます。

最終的な例

この例は、リスト アイテムを取得し、取得するプロパティを明示的に指定する方法を示しています。

 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();
于 2015-02-13T20:11:29.503 に答える