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 - http://blogs.technet.com/b/fromthefield/archive/2015/01/26/creating-a-subsite-web-using-csom-in-office-365.aspx. 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")
$Context.Load($Templates)
$Context.ExecuteQuery()
$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)
$Context.ExecuteQuery()

Brendan Griffin - @brendankarl

 

Comments (4)

  1. Great article - will like be of use for some for a few years to come 🙂

  2. bashr says:

    great article,
    only 1 thing not working. The global navigation of the created subsite is set to 'same navigation as parent site'
    But the navigation bar of the parent site is no there.
    If I set the global navigation to structural navigation, save and then set global navigation to 'same navigation as parent site' again. it works.
    Any idea if there is a solution for this?

  3. Just what I need says:

    Thanks for this great article - works perfectly.

  4. Rudy says:

    Thanks for this! However, I'm logged into the site and as I execute these lines, I do not see the sites anywhere. I look under subsites, but I don't see any visible changes.

Skip to main content