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 )

  • I assume you have an on-premise SharePoint Server and not SharePoint Online? – Zakir HC Apr 9 '15 at 10:16
  • Yep. I am taking about on-prem Sharepoint server 2010/13 – BND Apr 9 '15 at 10:25
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?

  • 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 9 '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 9 '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 9 '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
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, privacy policy and cookie policy

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