.WebTemplateID issues –.WebTemplateID doesn’t report what you’d think


*** Full blog post including screenshots for this issues can be found here: http://www.sp2010blog.com/Blog/Lists/Posts/Post.aspx?ID=49 ***
Whilst in the midst of creating the new move site PowerShell script (http://movesites.codeplex.com) I needed to find out the template id of a site that has been created. For instance:
Site A was made last year and we need to move it – to do this we need to:
• Export all the content from site A (to a folder somewhere as a .cmp file )
• Find out the original site template used to create site A
• Create a new site based on the original template
• Import all the site content from the exported (.cmp) file
So as you can imagine all the above looked pretty routine for a site move – until I started to investigate the WebTemplateID property.
It would appear that this property returns the web template ID of the very FIRST template that was used to create the site, so if a site has been built on a template from previous and not from scratch then it will report the initial template id and not the current template id.
So why all the fuss?
Well, basically in the early days of SharePoint creation even Microsoft used this method to create their templates. For instance they created a blank site (STS#1) which would have the initial template ID of 1 and then (rightly so to save effort) they made the team site (STS#0) based on the blank template, but yep you’ve guessed it – the underlying WebTemplateID remained as 1… This is also the case for document workspace (STS#2) which was also based on the blank template and thus has the same initial template ID of 1.
If we list all the templates from the site collections we can see the template ID issue. To display all the templates use this code (lovingly pilfered from the excellent http://get-spscripts.com):
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
This gives us the output of:

These are all the default templates and the IDs associated with them – you can clearly see that the template names are individual BUT the web template IDs (in the end column) are grouped in to the original templates that were used to create them.
This screenshot shows the groupings of the initial template IDs:

To illustrate the issue even further I created a TEAM Site at http://sp2010blog.com/export and then ran this snippet against it:
$web = Get-SPWeb http://sp2010blog.com/export
write-host "Web Template:" $web.WebTemplate " | Web Template ID:" $web.WebTemplateId
This returns the values of:
Web Template: STS | Web Template ID: 1
It’s clearly using the underlying site template value for a blank site…
….Still – why all the fuss? Can’t you just use the Template Name?
Well yes and no – if you are altering the script manually you can change the name manually but if you want to automate the process using a GUI for ease of use then you need to call the template ID programmatically and hence the problem… as calling the name only provides the first portion of the name (up to the #) and requires a separate call to get the id – so in essence it has to be built up, but can’t if the wrong id is reported.
The whole point of my the new move sites script is to provide a PowerShell, GUI driven script that can quickly and easily move a site from one location to another without having to edit code all the time – and more importantly do it for free rather than having to purchase very expensive third party products!
So far I haven’t been able to come up with an answer to this conundrum so anyone with any type of info or answer is more than welcome to post using the form on the right. The Move site scripts uses a drop down and link to this site for the end user to select the relevant template manually, but that is still a no go if you don’t know the source template…
One ray of sunshine however is that when you try to import a site into a newly created site made from a different template it bombs out and tells you which template you should be using in the first place which means the original template details are in there… somewhere… so if anyone has any ideas please get posting! 


HenridOrgeval wrote Jun 8, 2011 at 11:27 PM

Hi, consider having a look at my project spposter.codeplex.com.
Inside the PowerShell script file, search for function GetWebTemplateTitleForRootSite : this function exactly solves your problem !

wrote Jun 8, 2011 at 11:38 PM

Heath_Groves wrote Jun 9, 2011 at 8:16 AM

Henrid, thank you very much for the steer - this is exactly what i'm after. I'll review this later on today and look at pushing out a v1.1 soon as - this really has been the bain of my life! cheers!

cyberprune wrote Mar 23, 2012 at 6:35 AM

You should be able to use the Configuration property of SPWeb to detect the correct WebTemplateId. See http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.configuration.aspx

wrote Feb 14, 2013 at 2:56 AM