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 )

2
  • I assume you have an on-premise SharePoint Server and not SharePoint Online?
    – Zakir HC
    Commented Apr 9, 2015 at 10:16
  • Yep. I am taking about on-prem Sharepoint server 2010/13
    – BND
    Commented Apr 9, 2015 at 10:25

2 Answers 2

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?

6
  • 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
    Commented Apr 9, 2015 at 10:38
  • Yes it will work for SharePoint 2013 but not for 2010. Are you also looking for Server Object model?
    – Amit
    Commented Apr 9, 2015 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
    Commented Apr 9, 2015 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
    Commented Apr 10, 2015 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
    Commented Apr 11, 2015 at 12:56
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.