[英]sharepoint 2010. Include javascript in code for all item display forms
[英]SharePoint - Display Templates - Getting Every Item in All Lists in Site
我正在尝试从SharePoint网站内的所有列表中获取每个项目。 为了执行此操作,我找到的所有文档和回答的问题通常都说明了如何从一个列表中获取所有列表或所有项目,但并非网站上下文中所有列表中的每个项目。
我在下面的代码中能够找到所有列表,我最大的努力就是获取项目,而不仅仅是从最后一个列表中获取(出于某种原因,正如我在控制台中测试的那样,它一直在这样做-这个版本可能会只会产生null错误,因为我从之前进行了更改)。
var allInfo = "";
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getAllListsAndItems);
function getAllListsAndItems() {
var context = new SP.ClientContext('https://mysites.sprcompanies.com/personal/cnorman/charpractice/');
var web = context.get_web();
var lists = web.get_lists();
context.load(lists);
context.executeQueryAsync(onQuerySucceeded, onQueryFailed);
function onQuerySucceeded(sender, args) {
var listEnumerator = lists.getEnumerator();
while (listEnumerator.moveNext()) {
var list = listEnumerator.get_current();
allInfo += " List: " + list.get_title() + "\n";
if (list.get_itemCount() > 0) {
var query = new SP.CamlQuery();
query.set_viewXml('<View></View>');
var items = list.getItems(query);
context.load(items);
context.executeQueryAsync(onSuccess, onFail);
function onSuccess(sender, args) {
var itemsEnumerator = items.getEnumerator();
while (itemsEnumerator.moveNext()) {
var item = itemsEnumerator.get_current();
}
}
function onFail(sender, args) {
console.log('Request on items failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
}
}
console.log(allInfo);
}
function onQueryFailed(sender, args) {
console.log('Request on lists failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
}
我知道一般的问题区域在这里:
var itemsEnumerator = items.getEnumerator();
while (itemsEnumerator.moveNext()) {
var item = itemsEnumerator.get_current();
}
我最初将其附加到allInfo
,但它所做的只是产生“未初始化”错误。 我首先以为我只是没有正确加载项目,但是在控制台中对其进行测试之后,它确实显示了项目集合对象,因此这就是我认为与上面有关的原因。
我不能只使用for
循环来遍历项目吗? 我只需要每个项目的标题。 我在中尝试了for
和for in
,但是再次导致错误。 因此,这就是我访问每个项目的方式(使用错误的属性)。 先感谢您!
编辑:
因此,我将其放在onSuccess
块中:
if (items.get_item("Title") == null) {
items.get_data().forEach(function(item) {
console.log(item.get_item('URL').get_description());
});
}
else {
items.get_data().forEach(function(item) {
console.log(item.get_item('Title'));
});
}
无论是常规项目还是链接项目,两者都将获得项目的“标题”-问题在于,它仅获得最后一个列表的项目并重复多次,而不是遍历每个列表。
对于那些对我如何获得答案感兴趣的人:
var allInfo = "";
var listsArray = [];
var itemsArray = [];
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getAllListsAndItems);
function getAllListsAndItems() {
var context = new SP.ClientContext(SiteURL);
var web = context.get_web();
var lists = web.get_lists();
context.load(lists);
context.executeQueryAsync(onQuerySuccess, onQueryFailure);
function onQuerySuccess(sender, args) {
var listEnumerator = lists.getEnumerator();
while (listEnumerator.moveNext()) {
var list = listEnumerator.get_current();
listsArray.push(list.get_title());
}
for (var i = 0; i < listsArray.length; i++) {
var query = new SP.CamlQuery();
query.set_viewXml('<View></View>');
itemsArray[i] = lists.getByTitle(listsArray[i]).getItems(query);
itemsArray.push(itemsArray[i]);
context.load(itemsArray[i]);
}
context.executeQueryAsync(onSuccess, onFailure);
function onSuccess(sender, args) {
for (var i = 0; i < itemsArray.length; i++) {
if (listsArray[i] != "Master Page Gallery") {
allInfo += " List: " + listsArray[i] + "\n";
itemsArray[i].get_data().forEach(function(item) {
if (item.get_item("Title") == null) {
allInfo += " \t Item: " + item.get_item('URL').get_description() + "\n";
}
else if (item.get_item("Title") != null) {
allInfo += " \t Item: " + item.get_item("Title") + "\n";
}
else {
console.log("Something's wrong with this one.");
}
});
}
}
console.log(allInfo);
}
function onFailure(sender, args) {
console.log('Request on items failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
}
function onQueryFailure(sender, args) {
console.log('Request on lists failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
}
因此,基本上,我不得不将加载到数组中的每个列表都推入,然后使用该列表一次加载每个列表中的项目,因为最初循环遍历它是行不通的,因为它只能拾取最后一个列表。 这将产生如下结构:
List
Item
Item
...
List
Item
Item
...
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.