使用 CSOM 从 Sharepoint 列表中获取所有项目,包括查找值
Get all items from Sharepoint list including lookup values using CSOM
我想从 Sharepoint 列表中检索所有项目,包括查找字段的 ID 和值。
查找列表位于与查询列表相同的 site/url 上。目前我收到“属性 或字段尚未初始化”错误。
如何将查找字段正确加载到客户端上下文中,以便可以访问 属性?
在搜索和尝试不同的东西但没有成功之后,例如使用 list.GetRelatedFields() 获取所有相关字段并通过迭代相关字段集合中的列表 ID 加载所有列表中的所有字段,我需要帮助。
我再也见不到树木了。任何提示表示赞赏。提前致谢。
更新
发生了两件事。首先,我也忘记了将列表对象加载到上下文中。其次,我尝试请求配置错误的 property/column。
Sharepoint Online 上的列表是从另一个 Sharepoint 网站复制的,其中包括用于查找的列表的自动复制。
似乎此重复已损坏查找列。我不得不手动删除查找列并将其重新添加到请求的列表中。
现在,它起作用了! :)
using System;
using System.Collections.Generic;
using System.Linq;
using SP = Microsoft.SharePoint.Client;
namespace Theroka {
class Sharepoint {
private private SP.ClientContext context;
// credential handling, authorization, context creation, etc. ....
public void getItems() {
SP.List list = this.context.Web.Lists.GetByTitle("NameOfList");
this.context.Load(list); // UPDATE: I've forgot to load the list too
this.context.Load(list.Fields, c => c.Where(e => e.Hidden != true));
this.context.ExecuteQuery();
ListItemCollectionPosition position = null;
do {
CamlQuery query = CamlQuery.CreateAllItemsQuery(5000);
query.ListItemCollectionPosition = position;
SP.ListItemCollection items = list.GetItems(query);
this.context.Load(items);
this.context.ExecuteQuery();
position = items.ListItemCollectionPosition;
foreach(SP.ListItem item in items) {
// this works now
SP.FieldLookupValue lv = (item["LookupFieldName"] as SP.FieldLookupValue);
Console.WriteLine("{0}\t{1}\t{2}", item.Id, item["Title"], lv.LookupValue.ToString());
}
} while (position != null);
}
}
}
亲切的问候,
瑟罗卡
您可以尝试将列表项的所有字段作为文本加载到上下文中并只显示它们,我认为这是最简单的方法,但可能不是最有效的方法。在这种情况下,查找列也应该作为文本值(不是 id)可见。
将字段值包含为文本您需要将其与加载项目一起加载到上下文中。因此,对于您的示例,它类似于
this.context.Load(items, items => items.Include(item => item.FieldValuesAsText));
在那之后,您应该能够获得查找列的值,例如:
item.FieldValuesAsText["LookupFieldName"]
希望对您有所帮助
我想从 Sharepoint 列表中检索所有项目,包括查找字段的 ID 和值。 查找列表位于与查询列表相同的 site/url 上。目前我收到“属性 或字段尚未初始化”错误。
如何将查找字段正确加载到客户端上下文中,以便可以访问 属性?
在搜索和尝试不同的东西但没有成功之后,例如使用 list.GetRelatedFields() 获取所有相关字段并通过迭代相关字段集合中的列表 ID 加载所有列表中的所有字段,我需要帮助。
我再也见不到树木了。任何提示表示赞赏。提前致谢。
更新
发生了两件事。首先,我也忘记了将列表对象加载到上下文中。其次,我尝试请求配置错误的 property/column。
Sharepoint Online 上的列表是从另一个 Sharepoint 网站复制的,其中包括用于查找的列表的自动复制。
似乎此重复已损坏查找列。我不得不手动删除查找列并将其重新添加到请求的列表中。
现在,它起作用了! :)
using System;
using System.Collections.Generic;
using System.Linq;
using SP = Microsoft.SharePoint.Client;
namespace Theroka {
class Sharepoint {
private private SP.ClientContext context;
// credential handling, authorization, context creation, etc. ....
public void getItems() {
SP.List list = this.context.Web.Lists.GetByTitle("NameOfList");
this.context.Load(list); // UPDATE: I've forgot to load the list too
this.context.Load(list.Fields, c => c.Where(e => e.Hidden != true));
this.context.ExecuteQuery();
ListItemCollectionPosition position = null;
do {
CamlQuery query = CamlQuery.CreateAllItemsQuery(5000);
query.ListItemCollectionPosition = position;
SP.ListItemCollection items = list.GetItems(query);
this.context.Load(items);
this.context.ExecuteQuery();
position = items.ListItemCollectionPosition;
foreach(SP.ListItem item in items) {
// this works now
SP.FieldLookupValue lv = (item["LookupFieldName"] as SP.FieldLookupValue);
Console.WriteLine("{0}\t{1}\t{2}", item.Id, item["Title"], lv.LookupValue.ToString());
}
} while (position != null);
}
}
}
亲切的问候,
瑟罗卡
您可以尝试将列表项的所有字段作为文本加载到上下文中并只显示它们,我认为这是最简单的方法,但可能不是最有效的方法。在这种情况下,查找列也应该作为文本值(不是 id)可见。
将字段值包含为文本您需要将其与加载项目一起加载到上下文中。因此,对于您的示例,它类似于
this.context.Load(items, items => items.Include(item => item.FieldValuesAsText));
在那之后,您应该能够获得查找列的值,例如:
item.FieldValuesAsText["LookupFieldName"]
希望对您有所帮助