2

We have a console application (C#) that runs nightly to gather stats about our SharePoint Online environment. It has been logging the number of subsites we have using the SiteProperties.WebsCount property since 9/7/2015. Starting on 12/20/2016, that number has come back as zero instead of the actual value (which is 7,400+). We haven't changed this code since late November and all of the other stat values we gather are valid.

The property is documented here: https://msdn.microsoft.com/EN-US/library/microsoft.online.sharepoint.tenantadministration.siteproper...

I found a PowerShell script that uses the same approach as our console application here: https://gallery.technet.microsoft.com/office/How-to-get-all-the-tenant-2999c21b Below is a PowerShell version of this script that pulls the specific property I'm referring to, .WebsCount.

I've tried a couple of versions of CSOM: Microsoft.SharePointOnline.CSOM.16.1.4727.1200 and Microsoft.SharePointOnline.CSOM.16.1.5903.1200 .


    #Adding the Client OM Assemblies         
    Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.dll" 
    Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.Runtime.dll" 
    Add-Type -Path "<CSOM_Path>\Microsoft.Online.SharePoint.Client.Tenant.dll" 

    #SPO Client Object Model Context 
    $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl)  
    $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword)   
    $spoCtx.Credentials = $spoCredentials 
    $spoTenant= New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($spoCtx) 
    $spoTenantSiteCollections=$spoTenant.GetSiteProperties(0,$true) 
    $spoCtx.Load($spoTenantSiteCollections) 
    $spoCtx.ExecuteQuery() 

    #We need to iterate through the $spoTenantSiteCollections object to get the information of each individual Site Collection 
    foreach($spoSiteCollection in $spoTenantSiteCollections){ 

        Write-Host "Url: " $spoSiteCollection.Url " - WebCount: "  $spoSiteCollection.WebsCount
    } 

3 Answers 3

2

I just tried in our tenant with a basic test as such:

$site = get-sposite -identity "https://tenant.sharepoint.com/teams/eric" 
$site.WebsCount

This returned 0.

I then tried:

$site = get-sposite -identity "https://tenant.sharepoint.com/teams/eric" -detailed
$site.WebsCount

This returned 3.

Using your provided script worked fine. I was using the CSOM components from here. We are a first release tenant.

Url:  https://tenant.sharepoint.com/teams/site1  - WebCount:  1
Url:  https://tenant.sharepoint.com/teams/site2  - WebCount:  9
Url:  https://tenant.sharepoint.com/teams/site3  - WebCount:  1
Url:  https://teanant.sharepoint.com/teams/site4  - WebCount:  1

Edit to add: A few weeks ago I noticed an error come up in PowerShell when doing a get-sposite -detailed -limit all without any sort of change on our part.

WARNING: The -Detailed parameter will be deprecated for bulk enumeration.

Perhaps they have made some service changes under the hood and broke something. It might be worth putting in a service request in the portal or going directly to premier support if you have it.

2
  • Thanks Eric! I put in a service request this morning, thinking this is either a bug or an unannounced change. Based on your comment, its looks like they might have updated our tenant with an unannounced change. Commented Dec 21, 2016 at 13:56
  • The Microsoft Support Engineer was able to reproduce what I'm seeing and suggested I use a different method. I'm now getting different values for several properties for the same site collection, depending on which method I use: tenant.GetSiteProperties or tenant.GetSitePropertiesByUrl . The first one seems to be broken now. Commented Dec 22, 2016 at 20:40
0

You may have noticed that recently the SPO powershell has gone thru some changes. For one, it returns a much more complete set of sites. For e.g. it can now return personal sites, group sites, etc. which used to not be returned in the past. As part of this change, the -detailed parameter also went thru some changes.

  1. When the -identity parameter isn't specified, -detailed has no effect.
  2. When the -identity parameter IS specified, -detailed is implied.

Some of the SiteProperties, for e.g. WebCount are performance-intensive to compute, so when returning more than 1 site, it is no longer computed. Instead, you can enumerate ALL sites first with ('get-sposite -limit ALL') and then iterate thru specific sites ('get-sposite -identity <siteurl>') to obtain the WebCounts for them.

1
  • This is true -- but the old properties are still there (even though they return zero) and Microsoft hasn't updated the reference information on their site. Commented Jan 13, 2017 at 15:44
0

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 https://gallery.technet.microsoft.com/office/How-to-get-all-the-tenant-2999c21b

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.

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

Microsoft.SharePoint.Client.dll

Microsoft.SharePoint.Client.Runtime.dll

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

Microsoft.Online.SharePoint.Client.Tenant.dll

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.

//Code

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)
        {
            GetListOfSiteCollections();
            Console.ReadLine();
        }

        private static void GetListOfSiteCollections()
        {
            string SPOAdminSiteUrl = "https://tenantadmin.sharepoint.com";
            string SPAdminUserName = "[email protected]";
            string SPAdminPassword = "password";

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

                    //Below is what was needed earlier and we could retrieve WebsCount by iterating allsiteProps 
                    var allsiteProps = mytenant.GetSiteProperties(0, true);
                    context.Load(allsiteProps, aprop => aprop.Include(a => a.Url));
                    context.ExecuteQuery();

                    foreach (var s in allsiteProps)
                    {
                        //Below call is now required
                        var sprops = mytenant.GetSitePropertiesByUrl(s.Url, true);
                        context.Load(sprops);
                        context.ExecuteQuery();

                        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);
           }                   

           Console.WriteLine("Done.");

        }

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

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

Source: How to get WebsCount and other details from SharePoint online using Tenant Administration API

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.