Thursday, 5 January 2012

SharePoint 2010 - Get Web Template Name

Site templates provides a simple and quick way of provisioning new sites with common lists and library's.

However, in order to programmatically create a site from a template it is necessary to know the internal name used to reference the web template. 

The format of the internal name for a custom web template is a Guid followed by a hash followed by the template name.  You can find the name for the template using either PowerShell or C#.

PowerShell

The following PowerShell can be used to output the full template name for a given template.

$site = Get-SPSite -Identity http://sharepoint
$site.GetWebTemplates(1033) |  Where-Object {$_.name -like "*[TemplateName]*"} | Select Name

The following PowerShell can be used to output all the full templates within the site collection.

Get-SPWebTemplate | SELECT name

 

C#

The following C# can be used to find the desired web template given the templates name.

private string getWebTemplate(string SiteUrl, string WebTemplateName)
{
    string TemplateName = string.Empty;
    using (var site = new SPSite(SiteUrl))
    {
        using (var web = site.OpenWeb())
        {
            SPWebTemplateCollection templates = web.Site.GetWebTemplates(1033);
            foreach (SPWebTemplate template in templates)
            {
                string[] templateInfo = template.Name.Split('#');
                string CustomWebTemplateName = templateInfo[1];
                if (CustomWebTemplateName.ToLower().Equals(WebTemplateName.ToLower()))
                {
                    TemplateName = template.Name;
                    break;
                }
            }
        }
    }
    return TemplateName;
}

Once you have the full template name for the template you can then using it to programmatically create a site. Click here to see how to programmatically create a site using a web template.

1 comment:

  1. Use Regex.Is math with ignorecase , that will make you code more nicer

    ReplyDelete