5

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)?

1
  • How did you create you custom web template?
    – Aanchal
    Commented Sep 4, 2014 at 6:56

2 Answers 2

10

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.

0
0

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

3
  • As I wrote above this is the code I'm already using, but it doesn't give med the correct template names.
    – Thomas
    Commented Sep 4, 2014 at 7:19
  • @Thomas Blog#9 and Projectsite#6115 are custom templates that probably don't have a name specified. See reference on OOTB templates
    – Benny Skogberg
    Commented Sep 4, 2014 at 7:25
  • This is a straight OOTB sharepoint installation. I have not deployed any custom templates at all. If I look at webtemp.xml in 15/templates/1033/xml I can find <Template Name="BLOG" ID="9"><Configuration ID="0" ... So it is available though not returned through Get-SPWebTemplates.
    – Thomas
    Commented Sep 4, 2014 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.