Skip to content

Instantly share code, notes, and snippets.

@BrianTJackett
Last active September 30, 2021 12:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrianTJackett/fe20ea446e97dbc849f74d470e7ca65f to your computer and use it in GitHub Desktop.
Save BrianTJackett/fe20ea446e97dbc849f74d470e7ca65f to your computer and use it in GitHub Desktop.
Traverse all sites in SharePoint Online using C# CSOM.
List <SiteProperties> list = new List <SiteProperties>();
SPOSitePropertiesEnumerable ssp = null;
SPOSitePropertiesEnumerableFilter sspFilter = new SPOSitePropertiesEnumerableFilter();
SharePointOnlineCredentials creds = new SharePointOnlineCredentials("myUsernameGoesHere", securePassword);
using (ClientContext cc = new ClientContext("myURLGoesHere"))
{
cc.Credentials = creds;
String nextIndex = null;
Tenant tenant = new Tenant(cc);
//loop through all site collections including personal sites (even though not being used)
//borrowed this code from after decompiling SPO Management Shell assemblies
sspFilter.IncludePersonalSite = PersonalSiteFilter.Include;
sspFilter.IncludeDetail = true;
do
{
sspFilter.StartIndex = nextIndex;
ssp = tenant.GetSitePropertiesFromSharePointByFilters(sspFilter);
cc.Load(ssp);
cc.ExecuteQuery();
list.AddRange(ssp);
nextIndex = ssp.NextStartIndexFromSharePoint;
}while(nextIndex != null);
foreach (SiteProperties sp in list)
{
//DO YOUR WORK HERE FOR EACH SITE COLLECTION, such as looping through subwebs
cc.Load(cc.Web, w => w.NoCrawl,
w => w.Webs,
w => w.Url);
cc.ExecuteQuery();
//check subweb(s)
foreach (var subweb in cc.Web.Webs)
{
cc.Load(subweb, sw => sw.NoCrawl,
sw => sw.Url);
cc.ExecuteQuery();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment