Finding the customized template for a SharePoint web

Code Samples, SharePoint

When you save a site as a template and subsequently create new sites based on that template, there is no easy way to retrieve the customized site template the new sites were created from. This is because when creating a new site SharePoint starts from the base template of the original site and applies the customized site template on top.

The code below helps to overcome this limitation and retrieve the customized template name:

static String GetTemplateName(SPWeb web)
//1. detect features installed from custom solution.
SPFeature feat = web.Features.FirstOrDefault(x =>
null != x.Properties["GeneratedBySaveAsTemplate"] &&
Convert.ToString(x.Properties["GeneratedBySaveAsTemplate"].Value) == "1");

//2. if there are any, the site was provisioned using a custom template.
if (feat != null)
Guid solution = feat.Definition.SolutionId;
String name = web.Site.Solutions[solution].Name;
return name.Substring(0, name.IndexOf(".wsp"));
// otherwise, just return the base template.
return web.WebTemplate;



What do you think?