简体   繁体   English

SharePoint获取ContentType名称

[英]SharePoint get ContentType Name

I am trying to get All Folders and Files from a SharePoint library, executing a single Request. 我试图从SharePoint库中获取所有文件夹和文件,执行单个请求。

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. 但我不知道如何, FieldValues集合充满了数据。

ListItem详细信息

I see that the ListItem ContentType.Name is not initialized. 我看到ListItem ContentType.Name没有初始化。

ContentType Name未初始化

Is it possible somehow to update the query in a manner which will load the data for ContentType in this single call. 是否有可能以某种方式更新查询,该方式将在此单个调用中加载ContentType的数据。

Or the only possibility is to iterate through all files and do a load of ContentType for the specific file? 或者唯一的可能性是迭代所有文件并为特定文件加载ContentType?

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 . 但是我将在一次调用中获取此信息,如果可能的话,不在集合中迭代并开始多次调用ClientContext

PS: I am new to SharePoint programming. PS:我是SharePoint编程的新手。 I just want to fix a bug. 我只是想修复一个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. 正如您在SharePoint客户端对象模型(CSOM)中正确注意到的那样, ClientRuntimeContext.Load方法不会检索客户端对象的所有属性。

ClientRuntimeContext.Load Method has the following syntax: ClientRuntimeContext.Load方法具有以下语法:

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. 其中retrievals参数的目的是用于指定要被检索的属性。

Secondly, since SharePoint CSOM supports Request Batching , your example could be modified to this one: 其次,由于SharePoint CSOM支持请求批处理 ,因此可以将您的示例修改为以下示例:

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). 最后一个例子不起作用(在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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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