Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 36188

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

$
0
0

This post is a contribution from Mustaq Patel, an engineer with the SharePoint Developer Support team

The following code demonstrates getting the WebsCount and some other properties for site collection using the Tenant Administration API. The Tenant administration API comes with SharePoint Online Client Components SDK https://www.microsoft.com/en-us/download/details.aspx?id=42038

Assemblies to Reference

Microsoft.SharePoint.Client.dll

Microsoft.SharePoint.Client.Runtime.dll

The above two assemblies can be found at C:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions16ISAPI

Microsoft.Online.SharePoint.Client.Tenant.dll

This assembly can be found at  C:Program FilesSharePoint Client Components16.0Assemblies)

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 = "tenant-admin@domain.onmicrosoft.com";
            string SPAdminPassword = "password";

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

                    var allsiteProps = mytenant.GetSiteProperties(0, true);
                    context.Load(allsiteProps, aprop => aprop.Include(a => a.Url));
                    context.ExecuteQuery();

                    foreach (var s in allsiteProps)
                    {
                        //this is needed now, earlier WebsCount can be queried via Tenant.GetSiteProperties
                        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;
        }
    }
}

Viewing all articles
Browse latest Browse all 36188

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>