Monday, May 23, 2011

Finding site template names and ID’s in SharePoint using PowerShell

SharePoint 2010 includes the new Get-SPWebTemplate cmdlet for getting a list of site templates from the farm. You can use it by typing the following command (note that I am also sorting the table by template internal name):

Get-SPWebTemplate | Sort-Object "Name"


This can be quite useful, especially if you want to use PowerShell to create new sites with the New-SPSite and New-SPWeb cmdlets and need to know an internal name of the template that you wish to base the new site on.

You may also want to extend the use of the Get-SPWebTemplate cmdlet to provide additional information. For example, you may want to include the template ID in the table. This can be done by stepping through each template and adding the results to a PSObject, as follows:

function Get-SPWebTemplateWithId
$templates = Get-SPWebTemplate | Sort-Object "Name"
$templates | ForEach-Object {
$templateValues = @{
"Title" = $_.Title
"Name" = $_.Name
"ID" = $_.ID
"Custom" = $_.Custom
"LocaleId" = $_.LocaleId
New-Object PSObject -Property $templateValues | Select @("Name","Title","LocaleId","Custom","ID")
Get-SPWebTemplateWithId | Format-Table

Running this script will output the following table:


If you looking to find details of the site template used to create an existing site in a site collection, this can be done by attaching to the site through the SPWeb object and getting the WebTemplate and WebTemplateId property values, as follows:

$web = Get-SPWeb http://portal
write-host "Web Template:" $web.WebTemplate " | Web Template ID:" $web.WebTemplateId

This will produce an output similar to the text below:

Web Template: BLANKINTERNET | Web Template ID: 53

Finally, if you can’t be doing with all these internal names and ID’s and need to get a site template for use in a site creation script, you can use the display name (i.e., Title) of the template with the following command:

$template = Get-SPWebTemplate | where { $_.Title -eq "Team Site" }


Putting this all together, the following script will get the “Blank Site” template and create a new sub-site in an existing site collection:

$template = Get-SPWebTemplate | where { $_.Title -eq "Blank Site" }
$newWeb = New-SPWeb -Url http://portal/subsite1/subsite2 -Name "Subsite Test" -Description "This is a test site" -Template $template -AddToTopNav -AddToQuickLaunch -UniquePermissions
write-host "Site" $newWeb.Url "created at" (Get-Date).ToString()

You can get more details on the New-SPSite and New-SPWeb cmdlets from here and here respectively, or type get-help New-SPSite and get-help New-SPWeb from a PowerShell command prompt.

No comments: