gpt4 book ai didi

c# - Sharepoint 字段尚未在 C# 中初始化

转载 作者:行者123 更新时间:2023-11-30 13:22:25 25 4
gpt4 key购买 nike

我正在编写一个代码,它将遍历共享点列表中的每个列表项并查找空字段。如果发现空白字段,将通过电子邮件通知列表项的负责人。

我在行 val = oListItem[field.Title]; 中遇到错误

The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

在我看来,我已经初始化了该行之前的所有内容。

static void Main()
{
ClientContext context = new ClientContext("https://****");
context.Credentials = new NetworkCredential("****", "****");
List oList = context.Web.Lists.GetByTitle("TestBI");
FieldCollection fieldcol = oList.Fields;

context.Load(oList);
context.Load(fieldcol);
context.ExecuteQuery();

ListItem oListItem = oList.GetItemById(1);
object val = null;

for (int i = 1; i <= 4; i++)
{
oListItem = oList.GetItemById(i);
foreach (Field field in fieldcol)
{
val = oListItem[field.Title];
if(val == null)
{
//Send e-mail
}
}
}
context.ExecuteQuery();
}

最佳答案

欢迎来到 SharePoint CSOM hell 。

您确实加载了 List 和 FieldCollection,但您还必须加载每个 Field。事实上,您必须加载要从中获取属性的每个 SharePoint 对象。

for (int i = 1; i <= 4; i++)
{
oListItem = oList.GetItemById(i);

foreach (Field field in fieldcol)
{
context.Load(field);
context.ExecuteQuery();
val = oListItem[field.Title];
if(val == null)
{
//Send e-mail
}
}
}

编辑: 8 年过去了,我已经很久没有使用 SharePoint,但现在看到这个答案,我认为最好不要调用 context .ExecuteQuery() 在循环中。您可能应该使用第一个循环来 .Load 每个感兴趣的字段,然后调用 .ExecuteQuery,最后执行另一个循环来处理新加载的字段。

当涉及到网络请求时,请尝试大容量,而不是喋喋不休:努力限制您发出的请求数量(当然,只要它有意义)。

关于c# - Sharepoint 字段尚未在 C# 中初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23515915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com