Using the SharePoint 2010 WebTemplate Feature Element for creating Site Templates

In SharePoint 2007 it was possible to save a site as a site template with a .stp extension. Saving a site as a template can be done from the site settings user interface. The .stp file is, like most SharePoint specific file extensions, a .cab file. The .stp file contains a link to the original site definition that the saved site depended on and the delta between that site definition and the saved site.

In SharePoint 2010 we can still save sites as templates in much the same way. However, saving a site as a template won’t create a .stp file, but a .wsp SharePoint Solution file! The .wsp file doesn’t just contain the delta, but it contains the onet.xml of the original site definition and all features and elements and optionally also content. What’s new in SharePoint 2010 that makes this possible is the WebTemplate Feature Element. There isn’t a lot of documentation on the WebTemplate element yet. Basically all I could find was documentation related to Sandboxed Solutions, as you can use the WebTemplate Feature Element in Sandboxed Solutions. The most elaborate description can be found here: http://msdn.microsoft.com/en-gb/library/ee539417(office.14).aspx.

In short the story is that you can use the WebTemplate element to create a site template instead of the WebTemp.xml file that you would use to create a site definition. You can create site templates by either saving an existing site as a template, or you can create a site template using Visual Studio. If you create a site template by saving a site you can import the resulting .wsp package into Visual Studio. Note that if you do this that a lot of elements, like content types, site columns and properties from the property bag get imported into Visual Studio. Fortunately you can easily adjust the contents of the imported site template. You can clean up the template, but you can also make additional adjustments to it. Another benefit of importing the site template into Visual Studio is that it’s easy to put it under source control.

webtemplate example

The image above shows an example of what the WebTemplate element could look like. The most important thing to note from the image is that the WebTemplate has to reference the Template and the Configuration of the site definition the template is based on.

The debate about whether to use a site template or a site definition that was a lively one in SharePoint 2007 will probably continue to exist in SharePoint 2010. However with the introduction of .wsp site templates and the WebTemplate element site templates are now a whole lot more flexible and robust. One definite advantages of using site templates is that a site template can be deployed as a sandboxed solution. This means that the .wsp file can be deployed to the solutions gallery at site collection level. Anyone with site collection administrator rights can do this, so no access to the server is required. Only users within a site collection where the site template is deployed to can create sites based on the site template.
It’s also possible to deploy the same .wsp package as a farm solution to the farm’s Central Administration site. In order to do this a farm administrator that has access to the SharePoint server needs to add and deploy the solution to the farm. In this scenario the feature with the site template in it can activated on all site collections. Once the feature has been activated on a site collection users within that site collection can create sites using the template.

In the near future a lot more information on the new WebTemplate feature element will probably be published, but this is at least a short primer on what the element is and how to use it.
Have fun!