Does your project need rescued? Nearly 74% of projects do. Click here to view our Guide to get your project on the right path!

SharePoint 2013: Finding Sub Sites with SPServices

As I previously wrote when trying to find Document Libraries, sometimes you have to find sites and sub-sites in SharePoint without direct access to Central Admin, SQL, or PowerShell. Linked together with that issue, I needed to locate all of the sites and sub-sites that users had created then locate the document libraries that had been created in those sites.

Having created a function to list the libraries, we need a way to find all of the sub-sites branching beneath a given site. So what is our call to just get a listing of sites? If you reference SPService’s wiki you will see there are a series of methods for working with Web objects. What you will find is that there are two options GetWebCollection and GetAllSubWebCollection. The difference is, do you want all sub-sites of the current site you are on, or do you want every sub-site no matter how far down the rabbit hole they are? If you only want the current level then use GetWebCollection, if you want everything use GetAllSubWebCollection.

Here is the simple example to get only the sub-sites of a site.

jQuery().SPServices({
    operation: "GetWebCollection",
    //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 Web.
        jQuery(xData).SPFilterNode("Web").each(function(){
            //Append the current web's title and URL to the output Div
            jQuery('#outputDataDiv').append("<div class='webRecordTitle'> "
                                            +jQuery(this).attr("Title")+" : " 
                                            + jQuery(this).attr("Url")+" </div>");
        });
      }
});

 

Let’s expand on this and pull in our code from my last post to find every document library on every sub-site.

function getListCollection(webAddress){
    jQuery().SPServices({
        operation: "GetListCollection",
        //site URL passed into the function
        webURL:webAddress,
        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);
                    }
                });
            }
        });
}

jQuery().SPServices({
    operation: "GetAllSubWebCollection",
    //Your SharePoint Site URL goes here
    webURL:"https://www.yourSPUrl.com/Site"
    async: false,
    completefunc: function(xData, Status) {
        //For each xml node that is marked as a Web.
        jQuery(xData).SPFilterNode("Web").each(function(){
            //Append the current web's title and URL to the output Div
            jQuery('#outputDataDiv').append("<div class='webRecordTitle'> "
                                            +jQuery(this).attr("Title")+" : " 
                                            + jQuery(this).attr("Url")+" </div>");
            //Call the function to find all of the document libraries for this sub-site.
            getListCollection(jQuery(this).attr("Url"));
        });
      }
});

So just add in some CSS to make your output look nice, and you have a quick and dirty dynamic report of all user created document libraries all the way down the site structure.

Leave a Reply

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