in SharePoint 2010

Web Templates in SharePoint 2010: What You Should Know

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

image

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

image

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

image

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.

image

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.

image

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 FilesCommon FilesMicrosoft SharedWeb Server Extensions14TEMPLATESiteTemplatesstsxmlonet.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

image

However, we do supply Name and Title for our web template.

Once deployed, SharePoint i
dentifies 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)
}

Write a Comment

Comment

Time limit is exhausted. Please reload the CAPTCHA.

  1. 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!

  2. hi,
    thanks for the article. Can you give me a link/ soem sample cource code for,step by step procedure how to create a custom web template using VS 2012 for my SP 2013 ENV.[ or VS 2010 in SP 2010 env.]
    i want to create the same with site collection features, web features.
    i am having custom page layouts,custom master pages, custom web parts as site collec.features application pages.
    so, how can i start with VS 2012, where n where i want to edit the onet.xml file.
    help is appreciated !

  3. I try to create web template with farm scoped feature via visual studio 2012, but I found it could be deployed on CA successfully. I do this copy existing one web template module. I compared that both settings are same. why existing web template could only be display on site? My new created web template could not show up on the site collection.