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!

Comments -
  1. Gravatar

    Very cool, thanks for writing this up Mirjam. Interesting stuff. Making Site Definitions on the file system was a lot of work that required a dev resource. Making that easier would be wonderful. And .WSP could help with portability across farms and in multi-tenant setups.

      
  2. Gravatar

    I was doing this just today and was somewhat overwhelmed with all the files it generated. I think I'll be sticking to the programmatic approach with Feature Stapling on top of a base template to just do the differences. It'll be easier to maintain and will be more apparent what the additions are ;-)

      
  3. Gravatar

    Hey Mirjam,

    I think WebTemplate element features are a great addition in SharePoint 2010 and are the new gold standard for custom site deployments.

    I put up a post with some details and sample code on creating a WebTemplate solution in VS2010 from scratch. I am now using this as the base for most of my custom application development projects.

    www.sharepointbits.com/.../...-site-templates.html

      
  4. Gravatar

    ducnhjsm

      
  5. Gravatar

    Thanks for this article. I have a question here.

    How can I programmatically create a site collection in SharePoint 2010 based on a site template (which is saved as .WSP} ? Earlier in 2007, we could to it with .STP.

    Would appretiate your response.

    Regards,
    Anwar

      
  6. Gravatar

    Hi Anwar,

    From my site Definitions vs Web Templates post (sharepointchick.com/.../...s-vs.-webtemplates.aspx):

    You can also create a site based on a web template by using custom code. You simply use [Feature Guid]#[Web Template Name]. Spaces in the web template name are no problem.

    Cheers,
    Mirjam

      
  7. Gravatar

    Hi

    does Feature Stappling is Supported in Custom Web Template Feature Element for creating Site Templates ?

    Thanks in Advance.

      
  8. Gravatar

    Hi,

    Unfortunately it's not possible to use Feature Stapling with Web Templates in SharePoint 2010.
    Also check out this post:
    sharepointchick.com/.../...s-vs.-webtemplates.aspx

    Mirjam

      
  9. Gravatar

    One of the really exciting new options on the Feature Framework level of the SharePoint 2010 is the WebTemplate

      
  10. Gravatar

    Thanks Mirjam.

    Regards,
    Anwar

      
  11. Gravatar

    Power to the people. Web Templates (I sometimes like to call them "sandbox templates" just for fun) are great way to put power users in control of their site collections. Sorry developers, but it ain't all about us :-)

    Don't get me wrong, I've written a handful of really useful custom base definitions for publishing sites, but custom definitions really complicate your upgrade route. And with team sites the temptation is to make dozens of definitions to address every collaborative variation - but why not let Site Collection Admins manage their own variations? So if HR wants an extra page on their meeting sites but Sales doesn't, let them manage that themselves with a 'sandbox' template. Yes, I am both lazy and liberal...

    Jeremy, I get your point about staying in control of additions but isn't feature stapling against base site definitions a blunt tool? Unless maybe you really do want to make the same changes to all sites based on a definition (regardless of department or the preferences of the Site Collection Admins). Sure there are corporate policies that might demand that sort of thing. But for most occasions, why not just throw the feature out there and let SPweb or SPSite owners activate it if and when they want to?

    If we put some logic in the feature so that it only makes changes to webs where the base definition is, say STS#0, then we can still cascade down changes into sub-sites associated with that definition. The difference is that Site Collection Admins can decide whether or not to make those changes, rather than having to live with a farm-wide decision. It's all about managing your own tenancy. Though this approach would also require an event handler on newly created sites to deal with sites created after the feature is activated (same main feature though so not complex for a humble power user).

    Cheers, Darren.

      
  12. Gravatar

    Mirjam,

    I'm looking for a publishing site definition and also i need to staple few features. Thats teh reason why i'm goin with site definitions rather goin with site templates.

    Can you please give me the list of features we need toactivate for publishing site definition.

      
  13. Gravatar

    Hi Ramesh,

    If you mean by a "publishing site definition" a site definition that has the publishing features activated on it, you will need to activate the Publishing Infrastructure site collection feature and the Publishing site feature on it.

    However the easiest thing you can do is to take ONET.xml of the out of the box Publishing Site. You can find the ONET.xml for this type of site in the C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\SiteTemplates\PUBLISHING\XML folder. Simply use that as a starting point and change what you want to change.

    Hope that helps.

    Mirjam

      
  14. Gravatar

    Hi Mirjam,

    thank you for a great post.

    Please could you advice on how to scop the webtemplate to a site collection level. I dont want my web template to appear in the central admin's create site collection page.

    Also, i would like to enable my custom features after web is provisioned ... please could you advice what is the best approach to do this.

    Thanks in advance,

    Deepak

      
  15. Gravatar

    Hi Deepak,

    If you want to scope your web template to the site collection simply make sure you set the scope of the feature in which you deploy the web template to the Site scope.

    You can enable features on a site that is created by using a web template by adding the features in into the SiteFeatures or WebFeatures sections of the ONET.xml of your web template.

    More information on creating web templates can be found in this post:
    sharepointchick.com/.../...e-site-collections.aspx

    Hope that helps.

    Mirjam

      
  16. Gravatar

    Hi All
    I have developed one custom solution in sharepoint 2010 foundation. I have only one site collection, and the root site only, that is team site. i have developed DMS and other functionality as per customer requirement. Now I want to deploy on customer site. Which approch should I do? I want that customer must not be able to redistribute the solution once deployed to his farm.

    Regards
    -Chetan

      

Leave a Reply

 


Please add 1 and 2 and type the answer here: