Web Templates are a very flexible way to create site templates in SharePoint 2010. In this post I will go through some of the things one should take care of when building web templates. I will not be explaining what are web templates and how they stack up with Site Templates, Site Definitions. Vesa from Microsoft has already done a great job of explaining in detail.
Wayne Ewington from Microsoft did a good job of presenting about Web Templates at SharePoint Saturday New Zealand which helped me understand more about web templates.
Below are some interesting facts about web templates (in no particular order). This post will be updated regularly as I learn more about web templates.
Site Scoped or Farm Scoped
Web Templates can be associated with:
- Site scoped feature
- Web template is available for a particular site collection
- Farm scoped feature
- Web template is available for the full farm
For example: If you are building web templates for SharePoint 2010 Online, you will be deploying web templates as a site scoped feature in a sandboxed solution.
Site scoped web templates are usually available to create sub-sites from the Create New Site dialog page
Farm scoped web templates are available in the Central Administration Create Site page to create root site collections and also to create sub-sites from the Create New Site dialog page
Configuration ID
The Configuration element in the onet.xml file specifies the various lists and modules used when creating SharePoint sites. In Web Templates, you can only use Configuration ID 0
Site Scoped Feature – SiteFeatures Element in onet.xml
The SiteFeatures element contains references to site collection features to include in the site template.
In SharePoint 2010, if a feature listed in SiteFeatures element is not activated at the root site level, it is not activated during sub-site creation from the onet.xml.
So, having references to site collection features in a site scoped web template is no longer required, as none of those features are activated during sub-site creation.
Farm Scoped Feature – SiteFeatures Element in onet.xml
For a farm scoped web template, SiteFeatures element is parsed and SharePoint will activate the features (at the site collection level)referenced in the onet.xml. They are activated in the same order as in the onet.xml.
The difference with farm scoped feature is that you are creating a root site collection (from central administration) and thus for a new site, the SiteFeatures element is parsed. Of course, when you use the same web template to create a sub-site in that root site, the SiteFeatures element is ignored.
Avoid Modules – Use Features
If you are into building site definitions, then you are more inclined towards provisioning content to the site via Modules element in onet.xml.
Though Modules are supported in web templates, it is best to provision using Features. These features will be Web scoped and referenced in the WebFeatures element in onet.xml. Using features makes it more flexible and readable.
Also considering the upgrade paths, using features will be very beneficial.
Same thought should be given to ListTemplates and DocumentTemplates elements.
Upgrading Web Templates is Easy
Since you are using a feature to deploy web templates, it is now easy to upgrade web templates. This is the reason why you must consider moving away from Modules, ListTemplates, DocumentTemplates and use features wherever you can in web templates.
Upgrading to SharePoint vNext will be Easy
Yes, you read it right – Using web templates will make upgrading to SharePoint vNext easy!
The main reason being that web template is only used during the site creation and once the site is created, there is no reference that the site was created using a web template. The site does not reference the onet.xml used.
Store the Web Template ID in the Site’s Property Bag
Since the site created using a web template does not store any info about the web template, it is best practice to store that info so that we know which web template was used to provision the site.
Vesa’s post (linked at the beginning of the article) explains how to do this.
Start from a simple site template
When building web templates, always start from a very simple site template. For example – Blank site template or Team site template
My favourite is: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\SiteTemplates\sts\xml\onet.xml
Creating Sites Programmatically based on a Web Template
For site definitions, you will use the Site Definition ID (STS#0, CMSPUBLISHING#0 etc.,). But we don’t specify the ID for the web template
However, we do supply Name and Title for our web template.
Once deployed, SharePoint identifies your web template with the following:
{FeatureGuid}#WebTemplateName
FeatureGuid being the web template’s feature (site or farm scoped)
So you can now get your Web Template:
C#
SPWebTemplate webTemplate
= spSite.GetWebTemplates(lcid)
.Cast<SPWebTemplate>()
.FirstOrDefault(
wt => wt.Title == "Proseware Home"
);
PowerShell
For farm scoped web template:
$webTemplate = Get-SPWebTemplate | where {$_.Title -eq "Proseware Home" -and $_.LocaleId -eq "1033"}
For site scoped web template:
$spSite = Get-SPSite "http://site-url" -ErrorVariable err -ErrorAction SilentlyContinue if($spSite -ne $Null) { $webTemplate = $spSite.GetWebTemplates("1033") | where {$_.Title -eq "Proseware Home"} $spSite.RootWeb.Webs.Add("custom-site", "My Custom Site", "My Custom Site", "1033", $webTemplate, $false,$false) }
Is it possible to deploy the web template at WebApplication level?
I had that requirement so that the site template is not available for all the web applications in the entire farm
Any help on this will be appreciated.
Thanks In Advance!
@Guptha – Unfortunately, I don’t think you can deploy the web template at WebApplication level.