Office 365 Dev Tip - Get Subsite Count per Site Collection with the Office 365 (CSOM) API

Tobias Zimmergren
Tobias Zimmergren

Here’s a quick tip for when working with the .NET managed CSOM for Office 365 dev (with SharePoint Online). In a few cases and projects I’ve been involved with, I see code that iterates through each site collection and then iterates through each Web recursively in order to return a count of total subsites in the Site Collection.

Well, with the Client Site Object Model (CSOM) it is possible to fetch only properties for a site in your tenant. One of those properties is called “WebsCount“. Obviously this is only one of the properties and there’s a full list of the available properties in the end of this post as a reference.

All-in-all, considering performance in your applications is more important than ever when you have the latency of the clout to think about as well (not to mention throttling and other challenges).

Pre-requisites

In order to run the code in the snippet you should already have:

  • Installed the SharePoint Online CSOM NuGet into your Visual Studio project
  • Have an Office 365 tenant where you’re the Tenant Administrator (or have the sufficient permissions to get the Site Properties)

Office 365 dev – code snippet

Here’s a simple code sample to get the site property for WebsCount – you’ll be able to figure out the rest of the properties from here:

```

File: GetSiteProperties.cs

// Get the ClientContext of your SharePoint Admin site (https://yourdomain-admin.sharepoint.com) using (ClientContext context = new ClientContext("https://yourdomain-admin.sharepoint.com"))
{ // Create a new Microsoft.Online.SharePoint.TenantAdministration Tenant object and reference the client context of your Admin site Tenant tenant = new Tenant(context);

// Gets all the site properties for all existing site collections. 
var siteProperties = tenant.GetSiteProperties(0, true);
siteProperties.Context.Load(siteProperties);
siteProperties.Context.ExecuteQueryRetry();

// Do whatever you want with the returned properties. 
foreach (var siteProperty in siteProperties)
{
    // This is an efficient and easy way to find out the count of the subwebs in a site collection.
    Debug.WriteLine("Site Collection '{0}' has '{1}' subwebs", siteProperty.Url, siteProperty.WebsCount);
}

}

</noscript>If you want to know what other properties you can get, here’s the properties as defined with their correct accessors (get/set) in the Microsoft.Online.SharePoint.TenantAdministration SiteProperties.cs file:

<script src="https://gist.github.com/f91aa0f9829a37fb08b8.js"></script>

<noscript>```  
File: SiteProperties.cs  
-----------------------

public bool AllowSelfServiceUpgrade { get; set; }

public double AverageResourceUsage { get; }

public int CompatibilityLevel { get; }

public double CurrentResourceUsage { get; }

public DenyAddAndCustomizePagesStatus DenyAddAndCustomizePages { get; set; }

public DateTime LastContentModifiedDate { get; }

public uint Lcid { get; set; }

public string LockIssue { get; }

public string LockState { get; set; }

public string Owner { get; set; }

public PWAEnabledStatus PWAEnabled { get; set; }

public SandboxedCodeActivationCapabilities SandboxedCodeActivationCapability { get; set; }

public SharingCapabilities SharingCapability { get; set; }

public string Status { get; }

public long StorageMaximumLevel { get; set; }

public long StorageUsage { get; }

public long StorageWarningLevel { get; set; }

public string Template { get; set; }

public int TimeZoneId { get; }

public string Title { get; set; }

public string Url { get; }

public double UserCodeMaximumLevel { get; set; }

public double UserCodeWarningLevel { get; set; }

public int WebsCount { get; }  

Using something like the SharePoint Client Browser or simply debugging, you’ll easily be able to see the site properties if you just want to quickly review them:[![image](GHOST_URL/content/imagimageet/content/images/2015/07/image.png)

(You’ll see that it also includes the WebsCount, which enables you to easily see how many subsites you have in that Site Collection)

Enjoy.

Office 2013Office 365 APICSOMSharePoint

Tobias Zimmergren Twitter

Hi, I'm Tobias! 👋 I write about Microsoft Azure, security, cybersecurity, compliance, cloud architecture, Microsoft 365, and general tech!

Reactions and mentions


Hi, I'm Tobias 👋

Tobias Zimmergren profile picture

Find out more about me.

Recent comments

Mastodon