1
votes

I use "Site Settings" to "Save Site as Template", and then create a couple of sites using that custom site template (or should I call it a Solution?)

How can I get my C# to work out which sites were created from this custom site template?

Note that SPWeb's .WebTemplate, .WebTemplateId and .Configuration all come back "STS", 1 and 1 respectively regardless of whether the site was created using this custom template or not. I assume that that is because they are reporting on the underlying SharePoint out of the box template.

Thanks in advance.

3
Also, this question might actually be better suited for the SharePoint StackExchange site: sharepoint.stackexchange.comGlenn Ferrie
Yep. I asked there first, but it still only has had 2 views, and I think both of them were mine. I thought I'd try my luck here since it was a programming question.Swanny
Correction. Apparently I posted to SharePoint meta site instead. Now I know what the meta bit means.Swanny

3 Answers

1
votes

Why not add an entry into the web's PropertyBag? With a key of "web_template_id" and the ID or name of the template as the value. You can do this easily with SharePoint Designer and the PropertyBag is included as part of the web template WSP.

Alternatively, if you import the template into Visual Studio you can add an entry into the web's property bag declaratively, like so:

<Property Name="web_template_id" Value="SiteTemplates.TemplateName" Type="string" />

It's then a simple case of fetching the custom template name from the AllProperties collection of the SPWeb object in your code.

0
votes

From http://www.go4sharepoint.com/Forum/determine-site-template-site-11323.aspx

You can use a combination of the WebTemplate and Configuration properties of the SPWeb object to find out which template the site was originally built from. The WebTemplate will contain something like "STS" which is the name of the subdirectory in the 12 hive Template/sitetemplates directory containing the site definition. The Configuration will correspond to the Configuration ID inside the onet.xml in that subdirectory. However there are a couple issues with this.

This is a reference that is pertains to SharePoint 2007, but I believe the core concepts still apply, except for any reference to the "12" hive, should refer to the "14" hive.

0
votes

To understand what is happening, you need to understand the difference in SharePoint between a Site Definition and a Web Template:

Although often used interchangeably, they are different and are treated differently within SharePoint. Site Definitions, whether out of the box or custom, live in 14 hive\TEMPLATE\SiteTemplates. On the other hand, Web Templates save customizations to an instance of a Site Definition to a .stp or .wsp file, which allows that site to be replicated in other site instances.

So, here is the real problem: even Microsoft gets confused in the difference between Site Definitions and Web Templates. Because when they named the WebTemplate property, they meant Site Definition, not Web Template.

Notice what MSDN says:

SPWeb.WebTemplate Property

When you create a custom site template by saving a site as a template and then create a new site from that template, the WebTemplate property contains the name of the site definition from which the custom template derives, not the name of the custom template. Thus if the site that was used to create a custom template was itself created from the standard team site definition, the WebTemplate property of all sites that are created from the new template will return "STS"

SPWeb.Configuration Property

When you create a site template by saving a site as a template and then create a site from that template, the Configuration property contains the ID of the site definition configuration from which the site template derives.

(emphasis mine)

This supports what you discovered in your question. So how do you determine what custom Web Template was used to create a site? I'm not sure it is possible. Because if it was possible, I would hope that MSDN would have provided another sentence or two telling how to accomplish what they just explained these properties don't do. Also, in searching around, I have found this question asked a couple times, but there has never been a satisfactory answer.