Using Web Templates to create Site Collections

 

After writing my post about site definitions and web templates a lot of people have asked me whether you can use web templates to create site collections. The short answer is ‘yes, you can’. This post will walk through the steps of how to actually do that. In this post I will build a very basic web template, the point of the post is how to use web templates to create site collections. If you want more in-depth information on how to create a web template take a look at Vesa’s post.

To create a web template we start by opening up Visual Studio. We will create an “Empty SharePoint Project”. I will call mine “MyTemplate” but you can obviously give it any name you like.

Create an Empty SharePoint Project

We will start of by deploying the solution as a sandboxed solution. Make sure the URL you choose to deploy the solution to actually points to an existing SharePoint site on the machine you are working on.

Deploy as Sandboxed Solution

RIght-click on the project name in the solution explorer and select Add > New Item. Choose to add an Empty Element and give it a nice name. I will call mine “MyCoolWebTemplate”. I would advise you to choose a name that doesn’t contain any spaces.

Add Empty Element item

Visual Studio will automatically add an Elements.xml file to your empty element. In here we can define the metadata of our web template. For those of you who have built site definitions in the past, this file will replace the webtemp*.xml file that a site definition uses. In order to allow us to use an elements file to specify a web template a new XML element was introduced with SharePoint 2010, the WebTemplate element. The WebTemplate element can contain a lot of attributes, but we don’t need all of them.

A site created using a web template will only use that web template while the site is being provisioned. After that the site will think it is based on the site definition and configuration listed in the BaseTemplateID, BaseTemplateName and BaseConfigurationID. These attributes have to be filled in for the web template to work properly. You should base your web template on an out of the box site definition that is as close to the web template you’ll be creating as possible. In my case I have chosen to use the standard Team Site site definition. This site definition won’t actually be used when the site is being provisioned, but it is what the site’s SPWeb.WebTemplate, SPWeb.WebTemplateID and SPWeb.Configuration will return if queried after the site has been created. I know this is confusing, but it’s important to wrap your head around it, in order to understand how web templates work.
A big plus of this approach is, that when at some point you want to migrate your site to SharePoint vNext you don’t have to rebuild your custom site definition for vNext. The site looks like it was created based on a standard site definition and modified using the UI afterwards. This means it should be easier to upgrade the site than if you had created your own custom site definition.

The Name attribute of the WebTemplate element will have to exactly the same as the name of your Empty Element in Visual Studio. If these two don’t match the web template won’t deploy properly.

The Title, Description and Display Category is what the user will see when he uses your web template to create a new site, so make sure those are clear and descriptive.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <WebTemplate BaseConfigurationID="0" 
                 BaseTemplateID="1" 
                 BaseTemplateName="STS"
                 Description="A cool site for cool people to share cool stuff" 
                 DisplayCategory="Cool sites"
                 Name="MyCoolWebTemplate" 
                 Title="Cool Site">
    </WebTemplate>
</Elements>

The next step is to add an ONET.xml file to the empty element in Visual Studio. You can use the ONET.xml from a standard site definition as a base, but you will have to make some adjustments to work around the fact that not everything you can use in a site definition is supported in a web template. I will be using the ONET.xml from the standard Team Site. You can find this in the C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\SiteTemplates\sts\xml folder. If you copy the ONET.xml file into your empty element make sure the Deployment Type is set to ElementFile. Depending on how you copy it in there, it might set to NoDeployment by default.

Deployment Type of ONET.xml file

Now open up the ONET.xml file to clean it up.

  • First step is to remove all configurations from it, except for one and give this configuration ID = 0.
  • Remove the <Modules> reference element from the configuration
  • Also remove all the <Modules> definitions from the ONET.xml
  • If the FileDialogPostProcessor, ExternalSecurityProvider or ServerEmailFooter elements are used in the ONET.xml file remove those

You might have noticed that when you added the empty element to your project, Visual Studio automatically created a feature for you. If you started with an empty project the feature will be called Feature1. The empty element will be automatically added to the feature as well. In my case the title of the feature is MyTemplate Feature1. I will change this into “Cool Web Template Feature”. If you want to you can also add a description. By default, the scope of the feature will be Web. Adjust the drop down to change the scope of the feature to Site. Now rename the Feature1 file to something more descriptive. I will rename it to “Site-CoolWebTemplateFeature”. Notice how I’m prefixing the feature with the scope it will be deployed at. You don’t have to do that, but I find that if your solution grows a bit bigger it is helpful to do so.

Site Scoped Feature

That’s it for now, we are ready to deploy our solution. Right-click your project’s name in the solution explorer and click Deploy. Visual Studio will now package your solution for you and because we chose to deploy it as a sandboxed solution it will deploy it to the Solution Gallery of the site collection you set at the deployment target when creating the project. Open up your browser and browse to that site. Go to Site Actions > Site Settings and click on the Solutions link in the Galleries section. The gallery should contain your sandboxed solution and it should be activated.

Solutions Gallery showing your sandboxed solution

 

Now go back to Site Actions and choose New Site. You should now see our Cool Sites category and in it should be the Cool Site template.

Create Cool Site

This confirms that we can now create a sub site based on our Cool Site template, but that wasn’t what we set out to do.
Open up Central Administration and go to Application Management > Create site collections. Fill in a title and a URL. You will notice that from the Template Selection control you can’t choose our Cool Site. That makes sense, because we deployed it as a sandboxed solution to a single site collection. That means it won’t be available when creating a new site collection from Central Administration. If we are using a hosted environment like Office 365 we will only be able to deploy sandboxed solutions, so we will have to make do with that. However we still might want to use our own templates for creating site collections.
Go ahead and select the Custom tab in the Template Selection control and choose < Select template later… >. Fill in a primary site collection administrator and click OK. A new site collection will be created.

Select Custom template

Browse to the newly created site collection. The site collection wasn’t fully provisioned, because we never selected a template for it. Because of that, browsing to the site will redirect us to the Template Selection page right away. Click on the Solution Gallery link on the page.

Link to Solution Gallery

Select the Solutions tab in the ribbon and click on Upload Solution.

Upload Solution into empty solution gallery

Click on the Browse button and select the .wsp file from the Debug or Release folder in the bin folder of your Visual Studio project. Activate the solution straight away.

Activate Solution

Use the ribbon to browse back to the site collection. You will now be able to select the Cool Site from the template selection control.

Select Cool Site template

If you do so and click OK, the Cool Site template will be used to fully provision our newly created site collection and you will be able to browse to it.

This is one way to use web templates to create site collections. However if you are working in an on-premise SharePoint environment where you can deploy full-trust or farm solutions and you have to create multiple site collections based on the same web template you might want to consider another approach.

Go back to Visual Studio and first clean up by right-clicking on your project’s name in the solution explorer and choose Retract. This will deactivate the feature and remove it from the site collection you deployed it to. Retracting the solution using Visual Studio won’t remove it from the site collection where you uploaded it to manually.
Next click on your project’s name in the solution explorer again and in the Properties pane change the value for Sandboxed Solution from True to False. Click Yes when you are challenged by a pop-up.

Project Properties

Now click on the feature you created earlier and change the scope from Site to Farm. If you want to be precise you should also rename the feature, to give it a farm prefix.

Farm Scoped Feature

Deploy the solution again by right-clicking on your project’s name in the solution explorer and selecting Deploy. Visual Studio will re-package your solution and because we chose to deploy it as a farm solution this time it will deploy it to the Farm Solutions Gallery in Central Administration. Go to Central Administration and click System Settings > Manage farm solutions. The manage farm solutions page will show your solution and it will tell you that the solution has been deployed.

Manage farm solutions page

Now go back to Application Management > Create site collections and fill in a Title and a URL. You will now notice that you can select your web template straight away from the template selection control on the create site collections page.

Select Cool Site template from Central Administration

Basically from a user interface perspective the web template now behaves exactly like a site definition would. Not only can you create site collections based upon it, but because the web template was deployed using a farm scoped feature site owners will also be able to use the web template to create sub sites from any site collection in your SharePoint environment.

So there you have it. You can create site collection based on web templates, both in a hosted and in an on-premise environment, using either a sandboxed, or a farm solution.

Comments -
  1. Gravatar

    Great article.

    I have a little query, you wrote - "You should base your web template on an out of the box site definition that is as close to the web template you’ll be creating as possible. In my case I have chosen to use the standard Team Site site definition. This site definition won’t actually be used when the site is being provisioned, but it is what the site’s SPWeb.WebTemplate, SPWeb.WebTemplateID and SPWeb.Configuration will return if queried after the site has been created."

    Do these site templates have Id's in the same way a defintion would? So that within code you can identify what template the site is? Rather than what it's been told it's based on.

    Cheers,

    Wes

      
  2. Gravatar

    Hi Wes,

    Thank you!
    You can 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.

    Mirjam

      
  3. Gravatar

    Hi Mirjam,

    In my solution, I am trying to provision the customWeb Template "TestTemplate" (Created using save as site template) on a site only if the parent site is also of the type "TestTemplate".

    So I am using the code :

    if(Web.WebTemplate == "TestTemplate")
    {
    // Code to add TestTemplate in the web's Template collection
    }

    The problem is , Since "TestTemplate" is based out of 'STS' I get Web.WebTemplate's value as "STS" and not the template I am looking for.

    Is there a way to determine if a web was created using a custom Web Template ?

    As per msdn documentation (link below) it may not be possible...
    msdn.microsoft.com/en-us/library/ms436902.aspx

    Is there any way you would suggest for this ?
    Looking forward to your reply !

    Thanks,
    Pallavi

      
  4. Gravatar

    Hi Pallavi,

    There is no way to determine whether a site or web template has been used to create an existing site, the only thing that's stored is the site definition they were originally based on.
    If you create a web template it's a best practice to add a feature that stores the web template used and its version in the property bag of the site, but if you are using save site as template to create your template you can't really add your own feature to that. Theoretically you could import the saved site into Visual Studio to add the feature, but I would advise you to recreate the template using Visual Studio to create the web template and then add the feature to add the necessary data to the property bag.

    Hope that helps.

    Mirjam

      
  5. Gravatar

    Hi Mirjam, small question:
    Is it possible to hide te webtemplate from te user interface in case you only want a administrator to use him with scripts? Thanks, Rutger

      
  6. Gravatar

    Hi Rutger,

    There is not "hidden" property for the WebTemplate element, like there is in the webtemp.xml files, but you can use the SharePoint user interface to hide web templates from it. You can do this by going into Site Settings and browsing to Page layouts and site templates under Look and Feel. In here you can select templates that can be used to create sub sites. Be aware that using this feature means you will have to enable new templates that can be used manually.

    Mirjam

      
  7. Gravatar

    Hi Mirjam,

    Thank you for this nice walkthrough...it is georgous!!!

    One samll question....;)

    <!--Property Bag Feature-->
    <Feature ID="{4dc959fb-eaad-4286-8df0-e7404c4635c2}" Name="Property Bag Feature">
    <Properties>
    <Property Key="GeneratedBySaveAsTemplate" Value="1" />
    </Properties>
    </Feature>

    Do I have to delete this lines from the Onet.xml also??

    Special thanks for your article again.
    Have a nice day

    by Christian

      
  8. Gravatar

    As a side note, you can create your web template within SharePoint and then use it to provision Site collections.

    This is pretty much as you said in your original article - once you've got your WSP it's just the same as above - but I thought it might be worth highlighting as it's a nice way of avoiding Visual Studio while making your Site Collection templates...

      
  9. Gravatar

    Thanks a ton !!
    I added a property bag to the site collection's root web and my solution now works fine!

      
  10. Gravatar

    Hi Christian,

    I never succeeded in adjusting a site that was saved as a template in Visual Studio and deploying it again, but I would assume you can savely remove that property from the ONET.xml. If you remove it and it breaks you can always put it back...

    Mirjam

      
  11. Gravatar

    Mirjam, Is featureactivation allowed. I need to bunch of thing after a site is provisioned. I follow ur article and everything works. Now i need to do something on feature activation. Please suggest.

      
  12. Gravatar

    Hi Amber,

    I'm assuming you are trying to use feature receiver. Feature receivers can be used in features in web templates.
    Be aware that only features in the ONET.xml of your web template get activated. Anything in th ONET.xml of BaseTemplate site definition or stapled to that site definition won't get activated.

    Can you provide more information if this wasn't the information you were looking for?

    Mirjam

      
  13. Gravatar

    Hello Mirjam,

    Is it possible to target only SiteCollections with a WebTemplate (= not listing the template when creating Sites)?

    Thanks,
    Bernhard

      
  14. Gravatar

    Hi Bernard,

    I don't think it's possible to target a web template to just be available for site collection creation as there is nothing similar to the WebTemp.xml file's RootWebOnly property available for a WebTemplate.

    Sorry for not being able to help.

    Mirjam

      
  15. Gravatar

    I'm beginner in SharePoint 2010. I read articles related to web templates but no where I found how can I create web template using my own site which I already customized.

      
  16. Gravatar

    Hi Mirjam,

    Great articles on WebTemplates, thanks.

    I have one issue, when I deploy my WebTemplate solution to the Farm Solutions, I see it is deployed but I cannot see the template when I come to create a new site collection or sub site.

    Do you know any common reasons why this might be?

    Thanks

    Mark

      
  17. Gravatar

    Hi Mirjam,

    Ignore my question. In my stupidity, I hadn't enabled the feature!!

    Thanks for the great article.

    Mark

      
  18. Gravatar

    Very nice and useful article for me!
    Thank you very much!

      
  19. Gravatar

    Hi,

    Is it possible to deploy a web template to only one web application in the farm? If not, how would you recommend that i activate features for a web or site template inside of a single web application? Please note, i cannot create custom site definitions.

    Thanks in advance!
    Carlos

      
  20. Gravatar

    Hi Carlos,

    Unfortunately is't not possible to deploy a web template to a specific web application. The best thing you can do is to create your web template in a site collection scoped feature and then create a solution that activates that feature on all sites within that one particular web application. It's far from ideal, but I don't really see any other way.

    Regards,
    Mirjam

      
  21. Gravatar

    There are several ways to do site provisiong in SharePoint for instance: Custom code Create site from web template Create site from site definition Create site from site template Vesa Juvonen has written a good article comparing different site...

      
  22. Gravatar

    There are several ways to do site provisiong in SharePoint for instance: Custom code Create site from web template Create site from site definition Create site from site template Vesa Juvonen has written a good article comparing different site...

      
  23. Gravatar

    Articles Sharepoint 2010 and web templates

      
  24. Gravatar

    Articles Article Author Short description Sharepoint 2010 and web templates

      
  25. Gravatar

    In general There are several ways to do site provisiong in SharePoint for instance: Custom code Create site from web template Create site from site definition Create site from site template Vesa Juvonen has written a good article comparing...

      
  26. Gravatar

    can i get the same thing in sharepoint 2007.

    create a new templates tab at template selection. under new tab custome templates.
    how can i do this?

      
  27. Gravatar

    Hi mohiuddin,

    In SharePoint 2007 web templates didn't exist yet. If you are working with SharePoint 2007 you can either create a site template by saving a site as a template from the UI (in 2007 this will create a .stp file), or you can use Visual Studio to create a site definition. You can find some information on site definitions here msdn.microsoft.com/en-us/library/ms434313.aspx.

    Hope that helps.

    Regards,
    Mirjam

      
  28. Gravatar

    Hello mirjam

    thaks for the reply.

    i fixed this issue by using Wsp builder.

    regards,

    Mohiuddin.

      
  29. Gravatar

    Articles Article Author Short description Sharepoi

      
  30. Gravatar

    I would be cool to post the contents on the ONET.xml up here to see. I keep getting errors when deploying my Solution.

      
  31. Gravatar

    I had created site template
    can you guide me
    how to attach property bag value to site definition

      
  32. Gravatar

    In general There are several ways to do site provi

      
  33. Gravatar

    Will you do the same with SP2013 and VS2013?

      
  34. Gravatar

    Using Web Templates to create Site Collections

      

Leave a Reply

 


Please add 8 and 6 and type the answer here: