Get all site collections from tenant using Tenant.GetSiteProperties()

shraddha sawant 1 Reputation point
2021-05-26T13:20:14.86+00:00

Could someone help me with implementation of GetSiteProperties to get all site collections from tenant.

What value we need to pass as start index. According to Microsoft documentation it is
An integer that represents the start index of the list of deleted sites.

https://learn.microsoft.com/en-us/previous-versions/office/sharepoint-csom/dn174792(v=office.15)

could you please confirm how many sites this method returns in one go.
For my application it is returning around 400 sites in first run with startindex as 0, which is very less than actual sites on tenant. And it is failing for any other value of startindex.
Could you please let me know if any updates pushed by Microsoft for this method.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,826 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Echo Du_MSFT 17,181 Reputation points
    2021-05-27T01:48:25.883+00:00

    Hello @shraddha sawant ,

    Welcome to Q&A Forum!

    The snippet using the "Tenant.GetSiteProperties" method o retrieve the site collection from the SharePoint Online Tenant is as follow:

    Tenant tenant = new Tenant(context);  
    SPOSitePropertiesEnumerable siteProps = tenant.GetSiteProperties("0", true);  
    context.Load(siteProps);  
    context.ExecuteQuery();  
    Console.WriteLine("Total Site Collections: " + siteProps.Count.ToString());  
    foreach (var site in siteProps)  
    {  
     Console.WriteLine(site.Title + "\t" + site.Template.ToString());  
    }  
    

    Disadvantage to this method is, it won’t return the Site Collections created on new modern site templates.

    We have another method under Microsoft.Online.SharePoint.TenantAdministration namespace for retrieving all site collections created under SharePoint online Tenant is Tenant.GetSitePropertiesFromSharePoint. The snippet given below uses this method and retrieve the all Site Collections irrespective of any webtemplates.

    var credentials = new SharePointOnlineCredentials(userName, password);  
    ClientContext context = new ClientContext(siteUrl);  
    context.Credentials = credentials;  
       
    Tenant tenant = new Tenant(context);  
    SPOSitePropertiesEnumerable siteProps = tenant.GetSitePropertiesFromSharePoint("0", true);  
    context.Load(siteProps);  
    context.ExecuteQuery();  
    Console.WriteLine("Total Site Collections: " + siteProps.Count.ToString());  
    foreach (var site in siteProps)  
    {  
    Console.WriteLine(site.Title +"\t"+ site.Template.ToString());  
    }  
    

    For more information, please read this post "Get All Site Collections from SharePoint Online Tenant".

    **Ps:**The link you provided appears to me as 404-Page not found.

    Thanks,
    Echo Du

    ==========================

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.