Identifying Template Name of Custom Site Template

Today is a new SharePoint challenge.

Requirement: Programmatically creating webs from a custom site template.

Challenge: Getting the template name of the custom site template since the OM for creating a web requires the template name as in the following:

  1. public SPWeb Add(  
  2.     string strWebUrl,  
  3.     string strTitle,  
  4.     string strDescription,  
  5.     uint nLCID,  
  6.     string strWebTemplate,  
  7.     bool useUniquePermissions,  
  8.     bool bConvertIfThere  
  9. )  

Where:

strWebTemplate

A string that contains the name of the site definition configuration or site template.

Reference: msdn

Solution

So we did our changes in the OOB team site, save it as a template and uploaded it to the Solution Gallery as in the following:

solution gallery
Figure 1. Our custom site template in Solution Gallery

From the UI we created a site using the new template and it worked fine. New the site has been successfully created with our changes from the new template.

So I thought we can get the WebTemplate property of the newly created site and using it in our code to create the multiple sites. But this doesn't happen as expected. The WebTemplate property of the new site returns the value “STS”, the template of the site definition of the site from where we did the “Save As Template”.

This is surprising because then we are not able to create the sites from our new site template since the API for creating a new web requires the template name. How will we get the exact template name so we can fulfill our requirements?

After digging into it more, we have executed the PowerShell command on our site collection as follows:

  1. $site = Get-SPSite “My Site collection URL”  
  2. $site.GetWebTemplates(1033) //local ID as parameter  

The preceding command will enlist all the web templates available for this site collection in the following format.

templates
Figure 2. List of available site templates for the given web for locale 1033

Then I could see that our template name is shown something like “GUID#MyWSPName” then to see the complete GUID I have executed the following command and it looks as follows:


Figure 3. PowerShell command to get the only web templates name

And the result is:


Figure 4. web templates name – Name of my custom web template

Then I really wondered from where this GUID came from and then I downloaded the WSP, renamed it to .CAB and extracted it. The folder structure looks as follows:

folder structure
Figure 5. Folder structure of extracting the my custom web template WSP

There is the folder for “WebTemplate” that contains the Onet.xml file and WebTemplate file. Basically site template is deployed as WebTemplate. I opened the feature.xml file and the feature id is the same as in the template name.

  1. <Feature Id="{b94442b8-e30c-4204-b085-57d12d781aad}" Title="Web Template feature of exported web template "  Version="1.0.0.0" Scope="Site" Hidden="FALSE" RequireResources="TRUE" xmlns="http://schemas.microsoft.com/sharepoint/">  
  2.     <ElementManifests>  
  3.         <ElementManifest Location="Elements.xml" />  
  4.         <ElementFile Location="resources\resources.fi-FI.resx" />  
  5.         <ElementFile Location="MyCustomSitetemplate\ONet.xml" />  
  6.     </ElementManifests>  
  7.     <Properties>  
  8.         <Property Key="GeneratedBySaveAsTemplate" Value="1" />  
  9.     </Properties>  
  10. </Feature>  

So I got the answer, basically it is the id of the feature that deploys the custom site template as a WebTemplate. Then finally I tried by creating a new web with this new template name and it was created successfully.

Thanks!

Feel free to comment or provide feedback if any, or if you have a query.