Get list of site collections using CSOM in Office365

Recently, while working on a SharePoint Online project for a client, came across a requirement to create a consolidated report from all site collections i.e. we need to access or get list of all site collections. Below code helps to get that:-

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
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s