Freigeben über


Get SPO sites with filter using SPOSitePropertiesEnumerableFilter

This post is a contribution from Aaron Miao, an engineer with the SharePoint Developer Support team

To get list of all OneDrive for Business sites of the tenant, with SharePoint Online Management Shell, run

 Get-SPOSite -Template "SPSPERS"

 

This will return all OneDrive for Business sites.

It can be done with CSOM APIs as well. In fact, there’s class called SPOSitePropertiesEnumerableFilter. This class makes it easy specially to get all specific type of sites.

As seen, the class SPOSitePropertiesEnumerableFilter is included in
Namespace: Microsoft.Online.SharePoint.TenantAdministration
Assembly: Microsoft.Online.SharePoint.Client.Tenant (in Microsoft.Online.SharePoint.Client.Tenant.dll)
You can get assembly Microsoft.Online.SharePoint.Client.Tenant.dll from latest version of nuget package: Microsoft.SharePointOnline.CSOM.

Code below shows how to use SPOSitePropertiesEnumerableFilter class to get different types of sites.

 using System;
using System.Collections.Generic;
using System.Security;
using Microsoft.SharePoint.Client;
using Microsoft.Online.SharePoint.TenantAdministration;

namespace GetAllSitesWithFilter
{
    class Program
    {
        static string TENANT = "mytenant";
        static string ADMINURL = "https://" + TENANT + "-admin.sharepoint.com";

        static void Main(string[] args)
        {
            //Specify tenant admin and site URL
            string adminUser = "admin@" + TENANT + ".onmicrosoft.com";
            SecureString password = "adminpassword".ConvertToSecureString();
            SharePointOnlineCredentials adminCred = new SharePointOnlineCredentials(adminUser, password);

            List<SiteProperties> list = new List<SiteProperties>();

            SPOSitePropertiesEnumerable ssp = null;

            using (ClientContext cc = new ClientContext(ADMINURL))
            {
                cc.Credentials = adminCred;

                String nextIndex = null;
                Tenant tenant = new Tenant(cc);

                SPOSitePropertiesEnumerableFilter sspFilter = new SPOSitePropertiesEnumerableFilter()
                {
                    // get personal sites
                    IncludePersonalSite = PersonalSiteFilter.Include,  // needed to for personal sites
                    IncludeDetail = true,
                    Template = "SPSPERS"

                    // get classic team sites
                    //IncludeDetail = true,
                    //Template = "STS"

                    // get modern sites
                    //IncludeDetail = true,
                    //Template = "GROUP"

                    // get communication sites
                    //IncludeDetail = true,
                    //Template = "SITEPAGEPUBLISHING"
                };

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

                    cc.Load(ssp);
                    cc.ExecuteQuery();

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

            foreach(SiteProperties siteProp in list)
            {
                Console.WriteLine(string.Format("Site Url: {0} - Template: {1}", siteProp.Url, siteProp.Template));
            }

        }
    }

    static class StringExtension
    {
        public static SecureString ConvertToSecureString(this string item)
        {
            if (!string.IsNullOrEmpty(item))
            {
                var secureString = new SecureString();
                foreach (var c in item)
                {
                    secureString.AppendChar(c);
                }
                return secureString;
            }
            return null;
        }
    }
}

As you can see, for getting all OneDrive sites, the filter can be set like this:

 SPOSitePropertiesEnumerableFilter sspFilter = new SPOSitePropertiesEnumerableFilter()
{
  IncludePersonalSite = PersonalSiteFilter.Include,
  IncludeDetail = true,
  Template = "SPSPERS" // template name for personal site
};

Note that IncludePersonalSite = PersonalSiteFilter.Include is needed to work with “SPSPERS” template filter. Without setting IncludePersonalSite, it returns no sites even if Template = "SPSPERS" is specified.
For getting all classic team sites,

 SPOSitePropertiesEnumerableFilter sspFilter = new SPOSitePropertiesEnumerableFilter()
{
  IncludeDetail = true,
  Template = "STS"
};

For getting all modern team sites,

 SPOSitePropertiesEnumerableFilter sspFilter = new SPOSitePropertiesEnumerableFilter()
{
  IncludeDetail = true,
  Template = "GROUP"
};

For getting all communication sites,

 SPOSitePropertiesEnumerableFilter sspFilter = new SPOSitePropertiesEnumerableFilter()
{
  IncludeDetail = true,
  Template = "SITEPAGEPUBLISHING"
};

Noticed that the sample code does not set Template filter like: Template = "STS#0". With or without “#0”, it returns same result.