CSOM-Get-list-of-SharePoint-sites-using-Paging

 When retrieving the list of site collections from SharePoint, you'll often run into paging where only the first 300 sites are returned.  In these cases, you need to leverage the NextStartIndex value to complete the paging cycle.  Here's how its done in C#:

string adminUrl = "MyTenant-admin.sharepoint.us";
ClientContext ctx = new ClientContext(adminUrl);
Tenant tenant = new Tenant(ctx);
List<string> sites = new List<string>();
SPOSitePropertiesEnumarable props;
int nextStartIndex = 0;
do
{
    props = tenant.GetSiteProperties(nextStartIndex, false);
    ctx.Load(props);
    ctx.ExecuteQuery();
    sites.AddRange(props.Select(x => x.Url));
    nextStartIndex = props.NextStartIndex;
} while (props.NextStartIndex > 0);
Console.WriteLine($"Found [{sites.Count}] sites!");

Happy coding...
C


No comments:

Post a Comment

Comments are moderated only for the purpose of keeping pesky spammers at bay.

SharePoint Remote Event Receivers are DEAD!!!

 Well, the time has finally come.  It was evident when Microsoft started pushing everyone to WebHooks, but this FAQ and related announcement...