SharePoint - 显示模板 - 获取站点中所有列表中的每个项目

SharePoint - Display Templates - Getting Every Item in All Lists in Site

我正在尝试从 SharePoint 站点中的所有列表中获取每一项。 我找到的所有文档和已回答的问题通常都会解释如何从 one 列表中获取所有列表或所有项目,但不是站点上下文中所有列表中的每个项目。

我的代码在下面,我能够很好地获取所有列表,我最大的困难是获取项目,而不仅仅是从最后一个列表中获取项目(出于某种原因,它在我在控制台中测试时一直这样做- 这个版本可能只会产生空错误,因为我从以前做了更改)。

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,但它所做的只是产生 'not initialized' 错误。我首先认为我只是没有正确加载项目,但在控制台中测试后,它确实显示项目 collection objects 所以这就是为什么我认为它与上述有关.

我不能只使用 for 循环来循环浏览项目吗?我只需要每个项目的标题。我尝试了 forfor 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')); 
    });
}  

无论是常规项目还是 link 项目,两者都会获得项目的 '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
     ...
...