SharePoint 2013 Apps: Get list of site collections using REST API

Let me clear that it is not possible to fetch a list of site collection neither via REST nor by using JSOM. But there is a workaround i.e. by utilizing SharePoint Search Query REST/JSOM API. For that a basic search site collection should already be configured and crawled. Then in our code, we only need to specify the query contentclass:sts_site to return sites results. Following is the working code snippet that I used to achieve that:
     
function GetAllSiteCollectionsInCurrentWebApp(currentUser) {

    var queryText = 'contentclass:sts_site';
    var url = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='" + queryText + "'";
    var executor = new SP.RequestExecutor(_spPageContextInfo.webAbsoluteUrl);
    executor.executeAsync({
        url: url,
        method: "GET",
        headers: { "Accept": "application/json;odata=verbose" },
        success: function (data) {
            //RetrieveSiteCollectioninWebApp(data, currentUser)
        },
        error: function (data) {

        }
    });
}


Sources:

  • http://www.sharepointnutsandbolts.com/2013/01/calling-sharepoint-search-using-rest-eg.html
  • http://stackoverflow.com/questions/25569281/what-is-the-rest-endpoint-url-to-get-the-list-of-site-collections-in-sharepoint?rq=1
Vaqar Hyder

E: vaqar.hyder3567@gmail.com

A:

Siebenbürgen Straße 2/1, Korntal-Münchingen, 70825, PK

Hire me on Freelancer.com

Comments

Popular posts from this blog

SPFx: Develop using SharePoint Framework without Installing all the dependecies.

SharePoint Online: Elevated Permissions....with love

Powershell: Filling up an Existing Excel Sheet with data from SQL Server