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:![]()
(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.
Comments are closed
Archived comments
Hi,
Does this require you already have the Web portion of a provider hosted app on something other than localhost? I get a 403 Forbidden when executing the query. Normally I provide credentials, but I was hoping this method could be used and a token stored somehow so I could create a service to queue up changes to be made.
Hi James,
You can actually use the above code snippets in a Console App, class library, web application, provider hosted app, azure webjob or whatever you please.
You can just reference the client side assemblies of SharePoint (if you're in Visual Studio, use NuGet and search for csom and you'll find them) - this way you can create a service which use a service account for example and you don't have to use a web context and you don't have to be targeting localhost.
The code I used above (albeit a bit simplified here) is actually targeting Office 365 in a project I've built, so the code is valid for that :-)