SharePoint Online: PowerShell to Create Subsite from Custom Template

Requirement: Create SharePoint Online subsite from custom site template using PowerShell.

How to create a subsite from custom template in SharePoint Online? 
To add a subsite in SharePoint online, follow these steps:
  • Sign-in to your SharePoint online site >> Click on Site Settings gear Icon >> Choose "Site Contents"
  • In site contents page, Click on "New" >> Click on "Subsite" link from the menu.
    sharepoint online powershell create subsite custom template
  • Provide the Name, description and URL to your new subsite. 
  • Under the "Template Selection" section, Select the template from "Custom" tab
    create subsite from custom template powershell
  • Specify other optional settings such as Permissions, Navigation, etc and click on"Create" button at the bottom of the page to create a subsite using specified site template in SharePoint Online.
Lets see how to create a site using custom site template in SharePoint Online using PowerShell.

SharePoint Online: Create Subsite from Custom Template using PowerShell
Assuming you have an existing site template "PMO site Template V2" created already, here is the PowerShell to create new site from template.
#Load SharePoint CSOM Assemblies
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"

#Set Config parameters
$SiteURL = ""
$SubsiteURL = "branding"
$SubsiteTitle = "Intranet Branding"
$SiteTemplateName="PMO Site Template V2"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

Try {
    #Setup the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Context.Credentials = $credentials

    #Get the Custom Site Template
    $SiteTemplates = $Context.Site.GetWebTemplates("1033","0")
    $Template = $SiteTemplates | Where {$_.Name -eq $SiteTemplateName}

    #Specify Subsite details and create subsite from custom template
    $Subsite  = New-Object Microsoft.SharePoint.Client.WebCreationInformation
    $Subsite.Title = $SubsiteTitle
    $Subsite.WebTemplate = $Template
    $Subsite.Url = $SubsiteURL
    $NewSubsite = $Context.Web.Webs.Add($Subsite)

    Write-host "Subsite Created Successfully!" -ForegroundColor Green
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
Here is my another post on SharePoint On-premises create subsite from custom template using PowerShell: PowerShell to Create Subsite from Custom Template in SharePoint

1 comment:

  1. I managed to get this working to create the subsite, however, it will not create the site using the custom template. I guess it just uses the defaults. I could delete the default folders and then use PowerShell to create them (as I use the template to create 6 folders on the subsite) but i'd like to get it working using the script above. It says completed successfully so i am not sure why its not building the subsite without using the template. Any ideas?


Please Login and comment to get your questions answered!

Powered by Blogger.