Wednesday, 12 February 2014

Identifying Template name of custom site template

Hello All,

Today, new SharePoint challenge. 

Requirement: Pro-grammatically creating the webs from custom site template.  

Challenge: Getting the template name of custom site template since OM of creating web requires the template name as

public SPWeb Add(
        string strWebUrl,
        string strTitle,
        string strDescription,
        uint nLCID,
        string strWebTemplate,
        bool useUniquePermissions,
        bool bConvertIfThere

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


So we did our changes in OOB team site, save it as template and uploaded in solution gallery as

Fig 1. Our custom site template in Solution Gallery 

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

So I thought we can get WebTemplate property of newly created site and using it in our code to create the multiple sites. But this doesn’t happen as expected. WebTemplate property of new site returns value the “STS”, template of the site definition of 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 API for creating new web requires the template name. Really wondering how we will get the exact template name so that we could complete our requirement.

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

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

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

Fig 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 following command and it looks like as follows:

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

And result is

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

I really wonder from where this GUID came, and then I downloaded the WSP, renamed it to .CAB and extracted it. Folder structure looks like as follows:
Fig 5. Folder structure of extracting the my custom web template WSP

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

<Feature Id="{b94442b8-e30c-4204-b085-57d12d781aad}" Title="Web Template feature of exported web template "  Version="" Scope="Site" Hidden="FALSE" RequireResources="TRUE" xmlns="">
                                <ElementManifest Location="Elements.xml" />
                                <ElementFile Location="resources\" />
                                <ElementFile Location="MyCustomSitetemplate\ONet.xml" />
                                <Property Key="GeneratedBySaveAsTemplate" Value="1" />

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


Enjoy reading J

Feel free to comment / feedback if any or if you have any query J

No comments: