如何从列表 Collection 中获取列表? getByTitle returns 空列表

How to get a List from a List Collection? getByTitle returns empty list

所以我从 host-web 中检索了一堆列表并将它们放入类似于 here.

概述的 listCollection 中

然后我尝试访问一个名为 TestList 的特定列表。但是,我无法使用 getByTitle("TestList") 从 listCollection 访问它。它会给我一个列表,其中没有我正在检索的列表中的信息。

但是,如果我循环遍历枚举器直到找到标题为 TestList 的列表,我就可以使用它。

有谁知道为什么这不起作用?

var testList = listCollection.getByTitle("TestList");
console.log(testList);

但是这样可以吗?

var listEnumerator = listCollection.getEnumerator();

while (listEnumerator.moveNext()) {

    oList = listEnumerator.get_current();

    if (oList.get_title() == "TestList") {
        console.log(oList);
    }
}

这些 console.log 产生各自的输出。看看最上面的 ClientObjectData 里面没有 Object。这是为什么?我错过了什么?

谢谢

使用 sp.js 中的 getByTitle 方法,你应该这样使用。

SP.ListCollection.getByTitle()

注意:这样你将只能得到列表名称而不是对象数组。

Returns the list with the specified title from the collection.

获取列表中的项目,您应该使用此方法。

SP.List.getItems()

Returns a collection of items from the list based on the specified query

来自SP.Listgetitemns docs

而不只是:

var testList = listCollection.getByTitle("TestList");

尝试

var testList = listCollection.getByTitle("TestList");
listItemCollection = testList.getItems(""); // or use a CAMLQuery to be more specific
context.load(listItemCollection);
context.executeQueryAsync(onQuerySucceeded, onQueryFailed);

然后它应该被填充并且应该看起来像第二个例子。