Get site collections from SharePoint Online

  1. //using Microsoft.SharePoint.Client;  
  2. //using Microsoft.Online.SharePoint.TenantAdministration;  
  3.   
  4. //string weburl = "https://site-admin.sharepoint.com";  
  5. //GetSiteCollections(credentials, weburl);  
  6.   
  7. //This method returns the count of Site Collections available in the tenant  
  8. //Also returns thr url of all site collections  
  9. private static void GetSiteCollections(SharePointOnlineCredentials credentials, string weburl)  
  10. {  
  11.     using (ClientContext ctx = new ClientContext(weburl))  
  12.     {  
  13.         ctx.Credentials = credentials;  
  14.         Tenant tenant = new Tenant(ctx);  
  15.         SPOSitePropertiesEnumerable siteCollections = tenant.GetSiteProperties(0, true);  
  16.         ctx.Load(siteCollections);  
  17.         ctx.ExecuteQuery();  
  18.         Console.WriteLine("Total Site Collections in tenant: " + siteCollections.Count.ToString());  
  19.           
  20.         foreach (SiteProperties site in siteCollections)  
  21.         {  
  22.             Console.WriteLine(site.Url.ToString());  
  23.         }  
  24.           
  25.     }  
  26.     Console.WriteLine("Press any key to exit...");  
  27.     Console.Read();  
  28. }