集合尚未初始化 [英] The collection has not been initialized

查看:83
本文介绍了集合尚未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to retrieve all items from a Sharepoint library with CSOM and create a list of it. I am sure it has something to do with the order of the code. The question is how?

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 对象,因此您的代码应如下所示:

You should have loaded the items object not the collListItems thus your code should look following:

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

这篇关于集合尚未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆