SharePoint CSOM, retrieving site collections. Limited to 300

csomsharepoint

I am trying to retrieve the site collections list from a SharePoint Online domain.

I am using C# and client object model.

The following code returns only 300 site collections.

var tenant = new Tenant(ctx);
spp = tenant.GetSiteProperties(0, true);
ctx.Load(spp);
ctx.ExecuteQuery();

Any idea on how to retrieve ALL site collections with CSOM ?

Thanks

Best Answer

I found the answer to this question,

the first parameter of the method GetSiteProperties is the index from which site collection retrieval starts.

I tried the the following command spp = tenant.GetSiteProperties(300, true);

which returned site collections from index 300.

So here is my code to get all site collections from sharepoint online

SPOSitePropertiesEnumerable spp = null;
var tenant = new Tenant(ctx);
int startIndex = 0;

while (spp == null || spp.Count > 0)
{
    spp = tenant.GetSiteProperties(startIndex, true);
    ctx.Load(spp);
    ctx.ExecuteQuery();

    foreach (SiteProperties sp in spp)
    siteCols.Add(new SiteCol(sp.Title, sp.Url));

    startIndex += spp.Count;
}

By the way, site collections are currently limited to 10000.

Related Topic