I am looking for a c# snippet for getting all OneDrive's. There is a lot of sample for PowerShell, but I am looking for c# snippet that can provide all OneDrives in a tenant.

  • what do you mean by "getting all OneDrive's" -- do you want a list users who have a OneDrive? Do you want a list of url's to each OneDrive? Do you want a list of files in each user's OneDrive? Do want to extract metadata or files?
    – willman
    Feb 13, 2020 at 16:28
  • Just the urls. I will test the answer provided tommorow. Thanks for your response. Feb 13, 2020 at 20:28

1 Answer 1


OneDrive's are simply specialized/personalized site collections in SharePoint that were created from the SPSPERS template. To find all OneDrives you can use Brian Jackett's technique for enumerating site collections with CSOM, just add an additional filter to only get sites based on the SPSPERS template using the SPOSitePropertiesEnumerableFilter.Template property.

Here is a sample (this is nearly identical to Mr. Jackett's, with the addition of the line sspFilter.Template = "SPSPERS") :

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 
    //borrowed this code from after decompiling SPO Management Shell assemblies
    sspFilter.IncludePersonalSite = PersonalSiteFilter.Include;
    sspFilter.IncludeDetail = true;
    sspFilter.Template = "SPSPERS";

        sspFilter.StartIndex = nextIndex;
        ssp = tenant.GetSitePropertiesFromSharePointByFilters(sspFilter);


        nextIndex = ssp.NextStartIndexFromSharePoint;
    }while(nextIndex != null);

    foreach (SiteProperties sp in list)
        //DO YOUR WORK HERE FOR EACH SITE COLLECTION, such as looping through subwebs
        // OR Logging to a file, or determining user ownership, etc.
        cc.Load(cc.Web, w => w.NoCrawl,
                                    w => w.Webs,
                                    w => w.Url);

        //check subweb(s)
        foreach (var subweb in cc.Web.Webs)
            cc.Load(subweb, sw => sw.NoCrawl,
                                        sw => sw.Url);
  • Thanks alot will test tommorow. Feb 13, 2020 at 20:29
  • Worked, thanks alot! Feb 14, 2020 at 6:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.