First published on TECHNET on Oct 11, 2016
This post is a contribution from Aaron Miao, an engineer with the SharePoint Developer Support team
Like myself, many of you may like the SharePoint Online API GetSiteProperties that lists all site collections of a SharePoint Online tenant. Unfortunately, as you know, the method was only available to SharePoint Online, although the document does not explicitly remark this.
Now the good news is that SharePoint team has made the same method available to SharePoint Server 2016 on-premises! You can get this from SharePoint 2016 October PU .
Here’s the sample code you could find online out there.
using Microsoft.SharePoint.Client; using Microsoft.Online.SharePoint.TenantAdministration; using (ClientContext ctx = new ClientContext("http://siteurl")) { ctx.Credentials = System.Net.CredentialCache.DefaultCredentials; SPOSitePropertiesEnumerable spp = null; Tenant tenant = new Tenant(ctx); int startIndex = 0; while (spp == null || spp.Count > 0) { spp = tenant.GetSiteProperties(startIndex, true); ctx.Load(spp); ctx.ExecuteQuery(); foreach (SiteProperties sp in spp) { Console.WriteLine(string.Format("Site: {0} -> URL: {1}", sp.Title, sp.Url)); } startIndex += spp.Count; } }
For SharePoint Online, the code will just work. However, for SharePoint 2016 on-premises, you need to follow the steps below to set environment.
Please follow Provisioning site collections using SP App model in on-premises with just CSOM post for a more details.
Here’re the required steps:
1. Install October 2016 PU for SharePoint 2016 to your SharePoint 2016 farm.
2. Download SharePoint Server 2016 Client Components SDK that to your remote development environment where you write your CSOM code This contains 16 version of the objects which are also needed with SP2016 on-premises. These are located in the Online.SharePoint.Client.Tenant.dll assembly, which is by default installed to C:\Program Files\SharePoint Client Components\16.0\Assemblies folder.
3. Enable Tenant admin server stub using just CSOM by running PowerShell script below.
$WebApplicationUrl = "http://webappurl" $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'} if ($snapin -eq $null) { Write-Host "Loading SharePoint Powershell Snapin" Add-PSSnapin "Microsoft.SharePoint.Powershell" } $webapp=Get-SPWebApplication $WebApplicationUrl $newProxyLibrary = New-Object "Microsoft.SharePoint.Administration.SPClientCallableProxyLibrary" $newProxyLibrary.AssemblyName = "Microsoft.Online.SharePoint.Dedicated.TenantAdmin.ServerStub, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" $newProxyLibrary.SupportAppAuthentication = $true $webapp.ClientCallableSettings.ProxyLibraries.Add($newProxyLibrary) $webapp.Update() Write-Host "Successfully added TenantAdmin ServerStub to ClientCallableProxyLibrary." # Reset the memory of the web application Write-Host "IISReset..." Restart-Service W3SVC,WAS -force Write-Host "IISReset complete on this server, remember other servers in farm as well."
4. Enable AdministrationSiteType property from one site collection in your on-premises farm which will be acting like a admin site. This could be, for example, root site in the root site collection of the web application where you call the new API on it.
$siteColUrl = "http://siteurl" $site = get-spsite -Identity $siteColUrl $site.AdministrationSiteType = [Microsoft.SharePoint.SPAdministrationSiteType]::TenantAdministration Write-Host "Enabling side-loading...." Enable-SPFeature e374875e-06b6-11e0-b0fa-57f5dfd72085 –url $siteColUrl
Note:
Namespace Microsoft.Online.SharePoint.TenantAdministration (in Microsoft.Online.SharePoint.Client.Tenant.dll) introduces new set of client APIs to SharePoint 2016 on-premises. However, please note that it is not identical to SharePoint Online version.
Welcome to the SharePoint Blog! Learn best practices, news, and trends directly from the SharePoint team.