【发布时间】:2020-11-07 12:41:51
【问题描述】:
我想从 Sharepoint 列表中检索所有项目,包括查找字段的 ID 和值。 查找列表与查询列表位于相同的站点/url。目前我收到“属性或字段尚未初始化”错误。
如何正确地将查找字段加载到客户端上下文中,以便可以访问该属性?
在搜索和尝试不同的事情但没有成功之后,例如使用 list.GetRelatedFields() 获取所有相关字段并通过迭代相关字段集合中的列表 ID 来加载所有列表中的所有字段,我需要帮助。
我再也看不到森林了。任何提示表示赞赏。提前致谢。
更新
发生了两件事。首先,我也忘记将列表对象加载到上下文中。其次,我尝试请求配置错误的属性/列。
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);
}
}
}
亲切的问候,
罗卡
【问题讨论】:
标签: c# sharepoint csom