SharePoint 2013: Finding Document Libraries with SPServices

Every once in a while you find yourself looking for a solution to an unusual problem. This one started off as a much more difficult reporting problem in a system where I had very limited access. I needed to find all the user created document libraries and then get a rough document count of which ones were being used.

As in other instances when I need quick and dirty custom listings, I looked at what SPServices could offer. Looking at their wiki you can see that there are a number of List related methods and calls. https://spservices.codeplex.com/wikipage?title=Lists

The one we need is GetListCollection. This will return a collection of all lists on a site. Once we have that collection we can filter it based on the list template type. You can see that every list type has a template code and a document library is 101. So now the function that will sort through the lists on a site, find all marked as document libraries, then filter out the built in document libraries (Style Library and Site Assets).

    jQuery().SPServices({
        operation: "GetListCollection",
        //Your SharePoint URL goes here
        webURL:"https://www.yourSPUrl.com/Site",
        async: false,
        completefunc: function(xData, Status) {
                //For each xml node that is marked as a List.
                jQuery(xData.responseXML).SPFilterNode("List").each(function(){
                    //filter by template code 101 = document library
                    // removing Style Library and Site Assets since they are built in libraries
                    if(
                        jQuery(this).attr("ServerTemplate")==101
                        &&jQuery(this).attr("Title")!=="Style Library"
                        &&jQuery(this).attr("Title")!=="Site Assets")
                    {
                        //Build the output html for the current document library
                        var outputListHtml = "<div class='listContainer'>"+
                                                "<span class='listTitle'>"
                                                +jQuery(this).attr("Title") 
                                                + "</span><br />";
                        outputListHtml += "Total Item Count: "+jQuery(this).attr("ItemCount")+" ";
                        outputListHtml +="</div>";
                        //Append the html to the output Div
                        jQuery('#outputDataDiv').append(outputListHtml);
                    }
                });
            }
        });

 

This will output the list of document libraries and their file count to a div with the id of outputDataDiv. As a note, this function assumes you have included jQuery and SPservices on the page in question.

Leave a Reply

Your email address will not be published. Required fields are marked *