c# - 集合尚未初始化

标签 c# sharepoint csom

我正在尝试使用 CSOM 从 Sharepoint 库中检索所有项目并创建一个列表。我相信这与代码的顺序有关。问题是如何?

ListItemCollection collListItem = oList.GetItems(camlQuery);
var newList = new List<Item>();
var items = oList.GetItems(camlQuery);
context.Load(collListItem);

context.ExecuteQuery();

foreach (var col in items)
{

    newList.Add(new Item()
    {
        ID = Convert.ToInt32(col["ID"]),

    });
}

我收到以下错误:

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

最佳答案

您应该已经加载了 items对象不是 collListItems因此您的代码应如下所示:

ListItemCollection collListItem = oList.GetItems(camlQuery);
var newList = new List<Item>();
var items = oList.GetItems(camlQuery);
context.Load(items);
context.ExecuteQuery();

关于c# - 集合尚未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37541669/

相关文章:

c# - 如何在 C# 中使用预定义的类委托(delegate)

共享点 2013 : Inserting image on page javascript csom

c# - 从部署在 Sharepoint 中的 aspx 页面调用自定义成员资格提供程序时出现问题

list - 如何启用专业?

javascript - 使用 JS 从 SharePoint 日历中检索事件编号

c# - 减去 2 个日期时间字段以获得剩余天数

C# Control.Invoke 方法组

c# - 打破 parallel.foreach?

Sharepoint:列表不存在。哪个列表?

javascript - 使用 Rest API 的 Sharepoint 列表项总和