How do I get the real Web template name and Id from a web?
-
29-09-2020 - |
Question
I'm creating a tool that will "clone" webs inside a site. It reads child webs of a set web and displays them as templates for the user to select from. I've however run into a problem when I try to create new webs using the template name and id gotten from the existing webs.
I'm using the following code to get the template name (of the form: STS#0):
//web is an SPWeb instance
var templateName = web.WebTemplate + "#" + web.WebTemplateId;
I've created three "template" webs using the standard sharepoint tool:
+--------------+-------------------------+------------------+
| Type | SharePoint TemplateName | My TemplateName |
+--------------+-------------------------+------------------+
| Team Site | STS#0 | STS#1 |
| Blog | BLOG#0 | BLOG#9 |
| Project Site | PROJECTSITE#0 | PROJECTSITE#6115 |
+--------------+-------------------------+------------------+
The SharePoint template name is what I've gotten from the available templates list using this code (PowerShell or LINQPad):
Get-SPWebTemplate -CompatibilityLevel 15 |? {$_.LocaleId -eq 1033}
web.GetAvailableWebTemplates(1033).Cast<SPWebTemplate>().Select(template => new {Name = template.Name, Title = template.Title}).Dump(1);
Shows that the template names i get correspond to thesse:
+------------------+---------------+
| Type | What is it |
+------------------+---------------+
| STS#1 | Blank Site |
| BLOG#9 | Doesn't exist |
| PROJECTSITE#6115 | Doesn't exist |
+------------------+---------------+
What is going on and how can I get the correct template names (name#id form)?
Solution
It seems I've been incorrectly using WebTemplateId where I should be using the Configuration property.
//web is an SPWeb instance
var templateName = web.WebTemplate + "#" + web.Configuration;
Using this code gives me the correct templateName I can use to create new webs.
OTHER TIPS
Find the template name of SharePoint site using PowerShell
$web = Get-SPweb http://SiteUrl
Write-host “Web Template:” $web.WebTemplate ” | Web Template ID:” $web.WebTemplateId
$web.Dispose()
To get a list of all web templates, use the following PowerShell code
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
Reference: Get a list of web templates and IDs in a SharePoint site