WebTemplates is the way to go

Hi!

In past project, when working in MOSS, there was the need to create site definitions from time to time. This was quite time consuming and a lot of time was spent writing xml. Even when copy/pasting some existing defintion there was some time before your site was up and running. Therefore, the introduction of WebTemplates with SharePoint 2010 was a huge improvement.

There are several people who have written blog posts about this subject in more detail than I will cover here. See the reference section at the bottom of the post for more information.

Compared with writing a site defintion, a web template will come up within the hour of starting the code. And using Visual Studio 2010 with the new SharePoint items will reduce the time spent on code to barely none. Using this approach will save huge amount of time.

So, lets get it on with an example

Creating a WebTemplate

Fire up Visual Studio 2010 and create a new SharePoint Project. Right click and from the SharePoint 2010 tab, add an Empty Element and give it a name that is somewhat meaningful. This element will add a feature to your solution, feature 1 if it is the first thing that happends in the solution, that will contain the WebTemplate element. Now, there are some considerations to make. First of all WebTemplates can be used both in Farm solutions and in Sandboxed solutions. If the scope of the feature is Farm, the WebTemplate will be available through the farm, and the solution must then be a Farm solution. If the feature scope is set to Site, the WebTemplate will only be available in the Site Collection where the feature is activated. This does not mean that the solution has to be Sandboxed but it can be. A good practice is to rename the feature to something more meaningful (I’, using Site-WebTemplates).

Now, edit the empty element created above. Rembember, WebTemplates is based on an already existing site definition. Use the out of the box definitions as much as you can. This being said, add the following xml snippet to the element.

<WebTemplate BaseConfigurationID="0" BaseTemplateID="1" BaseTemplateName="STS"
               Description="This is my own cool template that will do my bidding"
               DisplayCategory="My Own Templates"
               Name="MyOwnTemplate"
               Title="My own template">
</WebTemplate>

A short explanation to the above snippet;

  • BaseConfigurationID is the same as the Configuration for the site definition if looking in the SharePoint Root/Templates/1033/xml/*webtemp.xml.
  • BaseTemplateID is the ID of the template node found in SharePoint Root/Templates/1033/xml/*webtemp.xml.
  • BaseTemplateName is the friendly name given to the template node in SharePoint Root/Templates/1033/xml/*webtemp.xml.
  • Description is the text that will appear below the template selection box when creating a new site.
  • DisplayCategory is the name of the tab which will hold the template.
  • Name is the internal name of the template. This name must match exactly the name of the containing folder. This will be pretty much automatically happend if you are using Visual Studio the normal way :)
  • Title is the friendly name that the end users will see in the template selection box when creating a new site.

Below is the STS template (containing the Blank site, Team site and Document workspace), compare with the values in the above snippet to get a feeling for the connections with our new WebTemplate.

<Template Name="STS" ID="1">
    <Configuration ID="0" Title="Team Site" Hidden="FALSE" ImageUrl="/_layouts/images/stts.png" Description="A site for teams to quickly organize, author, and share information. It provides a document library, and lists for managing announcements, calendar items, tasks, and discussions." DisplayCategory="Collaboration" >    </Configuration>
    <Configuration ID="1" Title="Blank Site" Hidden="FALSE" ImageUrl="/_layouts/images/stbs.png" Description="A blank site for you to customize based on your requirements." DisplayCategory="Collaboration" AllowGlobalFeatureAssociations="False" >    </Configuration>
    <Configuration ID="2" Title="Document Workspace" Hidden="FALSE" ImageUrl="/_layouts/images/stdw.png" Description="A site for colleagues to work together on a document. It provides a document library for storing the primary document and supporting files, a tasks list for assigning to-do items, and a links list for resources related to the document." DisplayCategory="Collaboration" >    </Configuration>
 </Template>

Now we have the foundation laid, time to get to the details.

In order to get something up and running we will need an onet.xml and a default.aspx to hosts as our start page. Recommendation, use the onet.xml and default.aspx from the base template. In this case we are creating a Team Site (Configuration = 0, remember?) and therefore we will snag the onet.xml and default.aspx from the SharePoint Root\TEMPLATE\SiteTemplates\sts. Copy the files and paste them into Visual Studio in the Element folder. This copy/pasting stuff is a little dangerous at times. This will give us the files, but at the same time the files will not be included in the solution package and will give an error when deploying that the solution can not find the specified files. So, in order to remedy this, set the Deployment Type of each file to ElementFile.
Change the deployment type to get the files into the solution package

This far, we should have the following structure of the files in Visual Studio.
Structure in Visual Studio

Build the solution to see that everything seems ok.

Deploy the solution and activate your feature. This will give you the choice to create your new site.
Create a new site based on your own template.

This is pretty much as far as this blog post will go. And admit, it is a lot faster than setting up a site definition. Even though this example will only give you a Team Site, there are some quite easy steps to make heavy customizations to your site.

References:
Vesa Juvonen’s blog
MSDN – Deciding on WebTemplate or SiteDefinition
Mirjam’s blog

About these ads

About SharePoint stuff

SharePoint developer nerd that loves running, golf, the family...
This entry was posted in Features, SharePoint and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s