Get Site Templates in SharePoint Online using PowerShell

Requirement: Get Site Templates in SharePoint Online using PowerShell

How to Get Site Templates in SharePoint Online?
The "Template Selection" section in create site page gives you the list of site templates in SharePoint Online.
sharepoint online get web template

You need the site template in order to create sites in SharePoint. Picking the site template from web user interface is fine, but how to get site template in SharePoint Online from PowerShell?

PowerShell to Get Site Template in SharePoint Online
You need the site template ID when you create a site or site collection in SharePoint Online. Here is the PowerShell to get site templates in SharePoint Online. The Get-SPOWebTemplate cmdlet gets you the SharePoint Online site template list.
#Variables for Admin Center & Site Collection URL
$AdminCenterURL = "https://crescent-admin.sharepoint.com/"

#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL -Credential (Get-Credential)

#Get site template list
Get-SPOWebTemplate
This retrieves SharePoint Online site template ID list for PowerShell:
get site template sharepoint online powershell
While the above PowerShell cmdlet gives you the list of all OOTB site templates, how to get custom site templates?

SharePoint Online: Get Custom Site Templates using PowerShell
#Load SharePoint Online 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 parameter values
$SiteURL = "https://crescenth.sharepoint.com"

$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
         
#Get the Custom Site Template
$SiteTemplates = $Ctx.Site.GetWebTemplates("1033","0")
$Ctx.Load($SiteTemplates)
$Ctx.ExecuteQuery()

#Remove all users from the group
ForEach($Template in $SiteTemplates)
{
    $Template | Select Name, ID, Description
}

PnP PowerShell to Get SharePoint Online Site Templates
The "Get-PnPWebTemplates" cmdlet in SharePoint Online retrieves all default web templates. Lets use PnP PowerShell get site templates in SharePoint online, including custom site templates.
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Context & Web Objects
$ClientContext = Get-PnPContext
$Web = Get-PnPWeb

#Get All Web Templates
$WebTemplateCollection = $Web.GetAvailableWebTemplates(1033,0)
$ClientContext.Load($WebTemplateCollection)
$ClientContext.ExecuteQuery()

#Get the Template Name and Title
$WebTemplateCollection | Select Name, Title
This gets you the SharePoint online site template id list using PnP PowerShell.
Get Site Templates in SharePoint Online using PowerShell Get Site Templates in SharePoint Online using PowerShell Reviewed by Salaudeen Rajack on August 16, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.