Create a SubSite (Web) using a Custom Template in SharePoint Online using PowerShell and CSOM

Earlier this year I wrote a Blog post that demonstrated how to create Sub-Sites (Webs) within a SharePoint Online Site Collection using PowerShell and CSOM - Recently a couple of people have reached out to me to find out if it's possible to use this approach to provision Webs using a custom Template that has been uploaded to the Site Collection - I'll not get into the argument about whether you should beĀ using Custom TemplatesĀ or not! Here is a quick overview of how to do this.

Step 1 - Upload the Template to the Site Collection

Step 2- Execute the following PowerShell to list the templates available within the Site Collection (update the highlighted variables with the Site Collection URL and LCID - if this isn't 1033)

#Add references to SharePoint client assemblies and authenticate to Office 365 site
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$Username = Read-Host -Prompt "Please enter your username"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Site = "SiteCollectionURL"
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username,$Password)
$Context.Credentials = $Creds

#List Custom Templates
$Templates = $Context.Site.GetWebTemplates("1033","0")
$Templates | Where {$_.Name -like "*{*" } | Select Name, Description

This will return a list of all Custom Templates, in my case I have a single template, imaginatively named "MyCustomTemplate". The important piece here is the name, copy this as it will be used in the next step to provision a new Sub-Site.

Step 3 - Use the following to create a Sub-Site using the Custom Template, update the highlighted values accordingly.

#Create Sub-Web using the Custom Template
$WCI = New-Object Microsoft.SharePoint.Client.WebCreationInformation
$WCI.WebTemplate = "{516EB07D-8711-4B9F-A3AD-097B28C5CDDF}#MyCustomTemplate"
$WCI.Description = "MyNewSubSite"
$WCI.Title = "MyNewSubSite"
$WCI.Url = "MyNewSubSite"
$WCI.Language = "1033"
$SubWeb = $Context.Web.Webs.Add($WCI)

Brendan Griffin - @brendankarl