3

I am working on client application for which I don't want to use Server Object Model of SharePoint 2013. I am ready to go for anything other that SOM.

I want to get a complete list of site collections using either CSOM API or REST API or WebServices. Is it possible ? If yes, what is API from Client Object model ( CSOM )

BND
  • 71
  • 1
  • 2
  • 6

2 Answers2

6

Sahil Malik has written a blog post on how to "Get list of site collections using CSOM in Office365". It may help you.

Below is the code snippet from his blog:

var token = TokenHelper.GetAppOnlyAccessToken("00000003-0000-0ff1-ce00-000000000000", tenantAdminUri.Authority, null).AccessToken;
using (var clientContext = TokenHelper.GetClientContextWithAccessToken("https://yourtenant-admin.sharepoint.com", token))
{
     var tenant = new Tenant(clientContext);
     SPOSitePropertiesEnumerable spp = tenant.GetSiteProperties(0, true);
     clientContext.Load(spp);
     clientContext.ExecuteQuery();
     foreach(SiteProperties sp in spp)
    {
        // you'll get your site collections here
    }
}

Reference: http://blah.winsmarts.com/2014-4-Get_list_of_site_collections_using_CSOM_in_Office365.aspx

If you are looking for REST then using SharePoint Search Query REST Api you can achieve this, below is the sample code:

function searchSites(webUrl,success, failure) {
    var url = webUrl + "/_api/search/query?querytext='contentclass:sts_site'";
    $.ajax({
        url: url,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            success(data.d.query);
        },
        error: function (data) {
            failure(data);
        }
    });
}

//print sites info
searchSites(_spPageContextInfo.webAbsoluteUrl, function(query){
    var resultsCount = query.PrimaryQueryResult.RelevantResults.RowCount;
    for (var i = 0; i < resultsCount;i++) {
        var row = query.PrimaryQueryResult.RelevantResults.Table.Rows.results[i];
        var siteUrl = row.Cells.results[6].Value;
        console.log(JSON.stringify(siteUrl));
    }   
},
function(error){
    console.log(JSON.stringify(error));
});

Reference: What is the REST endpoint URL to get list of site collections?

Amit
  • 2,019
  • 1
  • 17
  • 23
  • I am new to SharePoint so may sound very basic question. But would it work for Sharepoint 2013 and 2010?. As it is for Office365. Also does it have equivalent .NET code ? – BND Apr 09 '15 at 10:38
  • Yes it will work for SharePoint 2013 but not for 2010. Are you also looking for Server Object model? – Amit Apr 09 '15 at 10:53
  • No.I am not looking for Server Object Model.I know it can be easily achievable using SOM. But it requires DLLs to be deployed on same machine where SharePoint Server resides. Anything other than that is fine , CSOM, REST API or even power shell. But I am not able to find documentation of Tenant class or SPOSitePropertiesEnumerable. I want to understand this code thoroughly and I believe it's clean solution to problem. – BND Apr 09 '15 at 10:59
  • 2
    Sahil malik told me that it is not doable for Sharepoint 2013 on prem. So I need to see some other alternative – BND Apr 10 '15 at 10:45
  • I am also not sure about on prem. Did you tried using REST, though not sure whether it will work or not. – Amit Apr 11 '15 at 12:56
  • It doesn't work. It only returns a subset of Sites. How to get the complete list using REST API (search API STS_Site I mean)? I am the owner of some sites which are NOT returned in the API call. What's the solution? – Syed Mauze Rehan Aug 01 '15 at 10:55
0

Using CSOM it is possible to get list of all site collections in sharepoint online. But in sharepoint on-premises(2016/2013/2010) it is not possible.