Wednesday, October 21, 2015

List SharePoint subsites on SharePoint page using webpart

If you would like to rollup the subsites under a SharePoint site use the below script and paste on a script editor webpart on any page.

This rollup using REST query also looks at the permission and displays site that the current logged in user has access to. You can change the URL to point to any subsite where you want to roll up the sites from.

<div id="resortslist"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>

$(document).ready(function () {

    GetSubSites();

});

    // function to check contract form status
    function GetSubSites()
    {

        $.ajax(
        {
            url: _spPageContextInfo.webServerRelativeUrl +
            "/_api/web/webs/?$select=Title,Created,Description,LastItemModifiedDate,Url&$filter=effectivebasepermissions/high%20gt%2032",
            type: "GET",
            headers: {
                "accept": "application/json;odata=verbose",
            },
            success: function (data)
            {
                readContractFormSuccess(data);
            },
            error: function (err)
            {
                alert(JSON.stringify(err));
            }
        }
        );
    }

    // success function for contract form check
    readContractFormSuccess = function (data)
    {
     
        var results = data.d.results;


        if (results.length > 0) {


            var html = "<div id='resorts-site-elements'><br/>";
            html += "<table>";
            html += "<tr><td  style='padding-right:10px>Resort Name</td></tr>"
            for (var i = 0; i < results.length; i++)
            {
                           
                     

                var created = results[i].Created;
                var descp =results[i].Description;
             
                var sitetitle =  results[i].Title;
                var link =results[i].Url;
                       


                html += "<tr><td style='padding-right:10px'>"
                html += "<a href='"+ link + "'>" + sitetitle + "</a>";
                html += "</td> ";
                 
                html += "</tr>";
            }
        }

        else {
            html += "<tr></td>No Sites</td></tr>";
        }

        html += "</table>";
        html += "</div>";
        $("div[id='resortslist']").html(html).show();
     
    }

 
</script>

No comments:

Post a Comment