How to get WebsCount and other details from SharePoint online using Tenant Administration API
Published May 01 2019 04:03 PM 865 Views

First published on TECHNET on Jan 18, 2017
This post is a contribution from Mustaq Patel, an engineer with the SharePoint Developer Support team

Prior to CSOM ver 16.1.4727.1000, we can get count of all sites in tenant in SharePoint Online Tenant using SiteProperties.WebsCount. You can find Powershell sample at below link

With CSOM ver 16.1.4727.1000 and SPO Service dated 12/20/2016 onwards, SiteProperties.WebsCount alone as in above Powershell will not return correct value. You will see WebsCount is returned as zero. In addition, other properties returned are either zero or not up to date. You can read more on the issue in below forum.

SharePoint Online Product Group changed the implementation of how these Tenant level API works due to performance reasons. Now if we want to retrieve WebsCount and other properties of a site collection, an additional call to Tenant.GetSitePropertiesByUrl is needed and the Property collection acquired from that call needs to be used to get WebsCount and other properties. Below is the working sample.

Assemblies to Reference



The above two assemblies can be found at C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI


This assembly can be found at  C:\Program Files\SharePoint Client Components\16.0\Assemblies)

Below is the code to get the webs count property.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.Online.SharePoint.TenantAdministration;
using System.Security;

namespace GetWebCountsSPO
class Program
static void Main(string[] args)

private static void GetListOfSiteCollections()
string SPOAdminSiteUrl = "";
string SPAdminUserName = "";
string SPAdminPassword = "password";

using (ClientContext context = new ClientContext(SPOAdminSiteUrl))
context.Credentials = AuthenticateMySPO(SPAdminUserName, SPAdminPassword);
Tenant mytenant = new Tenant(context);

SPOSitePropertiesEnumerable allsiteProps = mytenant.GetSitePropertiesFromSharePointByFilters(new SPOSitePropertiesEnumerableFilter
IncludePersonalSite = PersonalSiteFilter.Include,

context.Load(allsiteProps, null); // null means fill all properties

foreach (var s in allsiteProps)
//this is needed now, earlier WebsCount can be queried via Tenant.GetSiteProperties
var sprops = mytenant.GetSitePropertiesByUrl(s.Url, true);
Console.WriteLine("SiteCollectionURL:" + sprops.Url);
Console.WriteLine("WebsCount:" + sprops.WebsCount);
Console.WriteLine("StorageUsage:" + sprops.StorageUsage);
Console.WriteLine("TimeZoneId:" + sprops.TimeZoneId);
Console.WriteLine("LastContentModifiedDate:" + sprops.LastContentModifiedDate);
catch (Exception ex)
Console.WriteLine("Exception:" +ex.message);



static SharePointOnlineCredentials AuthenticateMySPO(string uname, string pwd)
var securePassword = new SecureString();
foreach (char c in pwd)

var onlineCredentials = new SharePointOnlineCredentials(uname, securePassword);
return onlineCredentials;

You can also read the above resolution on below blog

Version history
Last update:
‎Sep 01 2020 02:28 PM
Updated by: