4

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

Sylvain Gantois
  • 779
  • 1
  • 12
  • 28

2 Answers2

4

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.

Sylvain Gantois
  • 779
  • 1
  • 12
  • 28
2

I guess NextStartIndex didn't exist at the time this was asked, nowadays you can do:

SPOSitePropertiesEnumerable sites;
List<string> allSites = new List<string>();
int startIndex = 0;

do
{
    sites = tenant.GetSiteProperties(startIndex, false);
    ctx.Load(sites);
    ctx.ExecuteQuery();

    allSites.AddRange(sites.Select(s => s.Url));

    startIndex = sites.NextStartIndex;

} while (sites.NextStartIndex > 0);
Jussi Palo
  • 848
  • 9
  • 26