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 = "https://crescent.sharepoint.com/"
$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")
    $Context.Load($SiteTemplates)
    $Context.ExecuteQuery()
    $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)
    $Context.ExecuteQuery()

    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
SharePoint Online: PowerShell to Create Subsite from Custom Template SharePoint Online: PowerShell to Create Subsite from Custom Template Reviewed by Salaudeen Rajack on July 22, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.