Search code examples
javascriptsharepointdisplay-templates

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


I'm trying to get every single item from all lists within a SharePoint site. All of the docs and answered questions I've found in order to do this usually explain how to get all lists or all items from one list, but not every item in all lists within a site context.

I have my code below and I am able to get all lists fine, my biggest struggle is getting the items and not only from the last list (for some reason, it kept on doing this as I tested in the console - this version would probably just produce null errors instead since I made changes from before).

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());
            } 
        }

I know the general problem area is here:

var itemsEnumerator = items.getEnumerator();

    while (itemsEnumerator.moveNext()) {
        var item = itemsEnumerator.get_current(); 
    } 

I originally had it append to allInfo, but all it does is produce 'not initialized' errors. I first thought that I just wasn't loading the items properly, but after testing it in the console, it does display the item collection objects so that's why I think it has something to do with the above.

Couldn't I just use a for loop to cycle through the items instead? I only need the titles of each item. I tried a for and a for in, but it again results in errors. So it's really how I'm accessing each item (using the wrong properties). Thank you in advance!

Edit:

So I put this in the item onSuccess block instead:

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')); 
    });
}  

Both would get the 'title' of an item whether it's a regular item or a link item - the problem is that it only get the items of the last list and repeats those multiple times instead of going through every list.


Solution

  • For those interested in how I got the answer:

            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());
                }  
            }
    

    So basically I had to push every list that was loaded into an array then use that to load items from each one at a time because originally looping through it did not work since it only picked up the last list. This would produce a structure like this:

    List
         Item
         Item
         ...
    List
         Item
         Item
         ...
    ...