0

We are trying to fetch all site collections in our SP Online tenant using the below code.

SPOSitePropertiesEnumerable prop = null;
        prop = tenant.GetSitePropertiesFromSharePoint("0", true);
        ctx.Load(prop);
        ctx.ExecuteQuery();

        List<SiteProperty> AllSiteVals = new List<SiteProperty>();
        foreach (SiteProperties sp in prop)
        {
                flag++;
                SiteProperty siteDetail = new SiteProperty
                {
                    SiteTitle = sp.Title,
                    SiteUrl = sp.Url
                };
                AllSiteVals.Add(siteDetail);

        }

However we see a huge discrepancy in the number of site collections being fetched from code (around 1450) and from the admin center (around 3600). We verified that we are missing many site collections which we should not.

How do we fix this?

To fix this issue we also tried looping thru using prop = tenant.GetSiteProperties(flag, true);

Issue is we get 265 sites in the first iteration and then it moves out of the loop.

  • Are you sending tenant admin credentials to create tenant context ? – Siddharth Vaghasia Apr 24 at 5:21
  • @Avi do you have modern sites created in your tenant? – Asad Refai Apr 24 at 5:54
  • @SiddharthVaghasia - Yes I am using the tenant admin credentials to create the tenant context. – Avi Apr 24 at 6:01
  • @AsadRefai - We have both classic and modern sites in our tenant. We are missing sites from both modern and classic when fetching the site details using the code. – Avi Apr 24 at 6:02
1

You can use GetSitePropertiesFromSharePointByFilters method to get all site collections.

It can used as below:

List<SiteProperty> AllSiteVals = new List<SiteProperty>();
var tenant = new Tenant(ctx);
SPOSitePropertiesEnumerable props = null;

while (props == null || props.NextStartIndexFromSharePoint != null)
{

    SPOSitePropertiesEnumerableFilter filter = new SPOSitePropertiesEnumerableFilter()
    {
        IncludePersonalSite = PersonalSiteFilter.Include,  //use PersonalSiteFilter.UseServerDefault to exclude
        StartIndex = props == null ? null : props.NextStartIndexFromSharePoint,
        IncludeDetail = true
    };
    props = tenant.GetSitePropertiesFromSharePointByFilters(filter);

    tenant.Context.Load(props);
    tenant.Context.ExecuteQuery();

    foreach (var prop in props)
    {
        // do your stuff here
        // AllSiteVals.Add(props);
    }
}

Reference - Tenant.GetSitePropertiesFromSharePointByFilters method

List all communication sites in a tenant

  • This has fixed my issue. I am getting the same no. of sites as it was in admin center. Thanks for your help! Marking this as an answer. – Avi Apr 24 at 8:25
0

Please check the user account (this code is running under) has sufficient privileges to access all site collections in your tenant.

  • Yes the service account under which the code is running under is an admin to all the site collections. We are specifically adding it as site collection admin in case of any new site collections created. – Avi Apr 24 at 7:37

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.