Skip to content

How to Build a Site Collection Template from A Web Template in SharePoint 2010

First off, I should state that there’s really no such thing as a site collection template. What happens when a new site collection is created is that the collection gets created, and then a web template is applied to the root site. This whole process is actually decoupled in SharePoint 2010, and you no longer need to select a template when the site collection is created (as documented previously by Todd Klindt). Todd has a very clever solution to getting your web template to be used at the root site, but I recently had a requirement to have it fully automated, and to be visible to the templates available when creating a site collection in Central admin.

This was relatively straightforward in SharePoint 2007, you would save a particular site as a template, then go to the template gallery, download it to the file system on a front end server, and then run an STSADM command to have it added to the Site definitions list. However, in SharePoint 2010, site templates are no longer .STP files,they use .WSP solutions in the user solution gallery. That should be easy,right? Just save off the  WSP file, add the solution to the farm with either PowerShell or STSADM. The solution will actually install, but your site template won’t show up. The main reason is that the actual web template is scoped to “web” and for it to show up for site collections, it needs to be scoped for the farm.

The good news is that you can import a WSP file directly into Visual Studio 2010, edit it, and create a new solution that does work. I will now attempt to describe the relevant steps, and a few gotchas to do this.

1. Save The Site as a Template

Once you have your site looking and behaving the way that you want, it’s time to save it off.  Select Site Settings, and click Save Site As Template – It’s under the Site Actions section. Give the template a file name, name, and if desired, a description. Once you select OK, the solution will be created and saved to the solution gallery, which is at the root of the site collection. You’ll want to go there next, and you can do so by clicking on the “solution gallery” link in the successful save confirmation page, or by navigating to the root of the site collection, then selecting Site Actions – Site Settings and clicking on the Solutions Gallery link. There is no more Site Template Gallery!!!

image

2. Save the WSP File Locally

From the solutions gallery, Click the name of your solution, and you will be prompted to save the wsp file. Go ahead and do that, and then you need to delete the template from the gallery. Why? You’ll be ultimately registering it in the farm, and you don’t need any confusion as to which template to choose. As well, if you’ll be using the same name, this will lead to conflicts, so it’s best to eliminate it altogether.

3. Create a Project in Visual Studio

Open Visual Studio and create a new project. Make sure that you have the SharePoint 2010 Templates showing, and then select the “Import SharePoint Solution Package” template.

image

From the next screen select “Deploy As a Farm Solution”, and click Next.

image

Next, click Browse and navigate to the WSP file downloaded in 2.

image

Finally, ensure that all of the included items are selected, and select Finish.

image

Visual Studio will then import all of the items into the project. Now we’re ready to modify the project.

4. Modify The Feature Definitions

There will typically be 4 Features created for a site template, aptly named Feature1 through Feature 4. Personally, I like my feature names to be a little more descriptive than that. Feature 1 is for List Instances, Feature 2 is for Modules, Feature 3 is for the template itself, and Feature 4 is for the PropertyBag.

The purpose of our solution is to make the site template available, and the other features are effectively there in a supporting role. Currently, all 4 features are scoped to web, and all 4 are visible. In addition to renaming them, we want to hide features 1,2, and 4 from display, and we want to scope feature 3 to the farm. 

First, we’ll modify the Feature name. Simply Single Click (or right click and rename) on the Feature in the Visual Studio Solution Explorer, and enter a new name. You’ll notice that all of the supporting elements below are automatically renamed as well.

image

Next, We’ll want to modify the Title of the features. The title is what is used when the feature is displayed in the feature list. Although we’ll be hiding 3 of them, it’s still a good idea to use a descriptive title. Double click on the feature, and the Feature Definition box will open in the main window. Modify the Title, and if desired the Description field to something meaningful to your users.

Go ahead and repeat this step for all of the 4 features

With the feature definition in the main window, the feature properties should be loaded in the properties toolbox (if you don’t have it open, open it). We want to set the Is Hidden property to true for all of the features except for the Web Template feature itself (Feature 3).

image

The web template feature itself requires an additional modification, we need to change the scope from site to farm. We can do this in either the properties window or the feature definition window, but the feature definition window is a little more obvious.

image

5. Modify the Template Definition Itself.

This step may not be required if the template name and title will be identical to that which was saved initially. However, in this case, we have called the solution Sample 1, and when the template was saved, it was MyCustomSite. We need to modify the template itself to be called Sample 1, and we may have some additional tweaks.

From the Solution Explorer, open the Web Templates Folder and change the name of the Template by single, or right clicking on it. The feature definitions will be updated automatically.

image

Next, we need to open the Elements.xml file, and change the Name and the Title tag to the new name. Search and replace is a good idea here. Next, open the ONet.xml and change the Title attribute in the Project tag to an appropriate value, in this case, Sample 1.  This file can also be used to remove any feature dependencies that may not exist in the destination farm, but be careful – other elements of your template may be reliant on them.

6. Test the Project

Click on the Debug start button in Visual Studio and your template will be deployed to the server, and you’ll be prompted to create a new subsite. If you see your template in the custom tab, all is well. Go ahead and create a test site to make sure that everything is working. Visual studio may prompt you with deployment conflicts, just let it go ahead and resolve them automatically.

7. Deploy the Solution

We are now ready to deploy the solution to the farm. First, set Visual Studio to Release mode from the toolbar:

image

Then, Right Click on the project, and select Package

image

Then, you’ll need to navigate to your Visual Studio project folder, and then into the BINRelease folder. There you will find a .WSP file with the name of your project. You can either work with the file in place, or copy it to a folder, but next we will add the project to the Farm solution gallery. To do this, we will either use STSADM (old school) or PowerShell (the cool new way). I’m old, so I still use STSADM for solutions, but I’ll show both.

To add the solution using stsadm.exe, open up a command prompt (in administrator mode if UAC is turned on), and navigate to the folder containing the solution file. The syntax is:

C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14BINstsadm.exe –o addsolution –filename sample1.wsp

Where sample1.wsp is the name of your solution file.

To use PowerShell, open the SharePoint 2010 Management Shell, and enter:

add-SPsolution –LiteralPath c:solutionssample1.wsp

The argument for LiteralPath is the complete path to the solution file. If there are and spaces in the path name, it needs to be encased in quotes.

8. Deploy the Solution

Now that the solution has been added, it needs to be deployed. To do so, start central admin, Go to System Settings, and select Manage farm solutions in the Farm Management Section:

image

Click on your solution name, and then click the Deploy Solution button. Click the OK button on the following screen, and your solution will be deployed.

You can control whether or not the template is available by turning off the farm feature. You can do that from central admin by navigating to System Settings – Manage Farm Features. From here, you can turn your template on and off.

image

9. Create New Site Collection

At this point, we should be good to go. From Central Administration, navigate to Application Management, and click Create Site Collections. On the create screen, give the new collection a title, and a URL. Then, in the Template Selection, click the Custom tab, and your template should appear there.

image
 

Give it a site collection administrator, and click OK. After a few moments, the site will be created, and you can navigate to it.

image

Simple, right?

Well, Microsoft giveth and Microsoft taketh away. Adding the template to the farm is considerably more involved with 2010 than with 2007. However this approach is also a great deal more flexible – you can tweak that template to your heart’s content, and if you have ever tried to create a site template with 2007 using Visual Studio, you can really appreciate how Visual Studio 2010 makes it significantly easier.

31 Comments

  1. Brad Covell Brad Covell

    Thanks, Excellent. Exactly what I wanted to do and it works.

    Question: when I debug it or deploy it from Visual Studio I get a message about deployment conflicts, I automatically have VS resolve them but the deployment and/or debug fails and I””m asked to continue, if I continue to debug it works fine. Do I need to be concerned about these deployment conflicts? What is happening?

    When I package and deploy, it works great.

    Thanks man

  2. I get them too. I believe that this is happening because those elements already exist at the destination. If you like, you could pick through the solution and remove anything redundant, but I see little value in doing so, whereas you might screw something up.

  3. John,

    I followed this and everything looks fine except when I create new sites based off of the template the application pages (i.e. view all site content, site settings, etc.) all come back as blank pages. Any idea of what I am missing. I have 90K+ site collections based off of global site templates in 2007 that I am upgrading to 2010 and need to get these new web templates updated and working. Any assistance would be greatly appreciated.

  4. Darren Morgan Darren Morgan

    Really useful article. Ideally we would do all this in PowerShell, but changing the scope of the WSP in VS is not too bad to do (even for an IT Pro).

  5. John White John White

    When you say they came back as blank pages, do you mean that there was no content at all in the sites?

  6. mohamma siddiqali mohamma siddiqali

    Excellent learned something new

  7. Sandra Sandra

    Hi,
    i have site collections (blank template) that use default.aspx for the home page. For new site collections i wanted to use custom site template and i created one like you described. The problem occured when i deployed my solution – all the default.aspx from my existing site collections got replaced with the one from solution. I created my custom site template from blank template and team template, but in both cases the results were the same. Is this the expected behavior?
    Because of my branding i have to have custom default.aspx.

  8. aedla aedla

    Thank you for your article.

    When I tried to import the wsp file, I got message as “The SharePoint Solution import operation completed, but with errors.As a result, project may not deploy successfully”.

    Can you explain me how to resolve this?

    Regards,
    Ashok.

  9. Martin Martin

    Awesome!!!

    Many thanks,

    Martin.-

  10. RK RK

    Thanks a lot …it helped me a lot 🙂

  11. Anonymous Anonymous

    The simplicity of explanation is great!! exactly what was needed!
    Thanks a lot

  12. john liu john liu

    Great post, helped a lot here.
    Thanks.

  13. Do you happen to know where in the solution file one would change the image that displays when you go to “Site Actions > New Site”? I’d like to brand mine so it doesn’t just use the default New Site image.

  14. Marco Marco

    I don’t know what I’m doing wrong but this example simply is not working for me.
    During deployment I’m getting conflicts, chosing to resolve them automatically and this step fails. I have tried the simplest configuration: I’ve saved Team Site as a template, saved .wsp to file, deactivated and removed it, loaded into VS with success (and no errors) – and tried to deploy to another web application. Got conflicts, tried to resolve them automatically – got many messages in output window: “can not delete file…” for many conflicted files. And deploy failed.
    Any help I appreciate very much.

  15. harisha P harisha P

    i followed every step, but when i try to debug or deploy i’m getting the below erro “Error occurred in deployment step ‘Activate Features’: The system cannot find the file specified. (Exception from HRESULT: 0x80070002)”

  16. Great job with this tutorial! Much appreciated!

  17. moner@hotmail.com moner@hotmail.com

    I am customizing the Enterprise SEARCH site and that does not have a “Save Site as Template” – what do I do ?

    Thanks.

  18. androcar androcar

    Hi,

    First of all great job with this toturial ! But I have a problem, I’m missing the “save site as template” under the Site Actions section … I only find it at Central Administration, but there I don’t have anything …
    I’m new at SharePoint 2010, maybe I’m missing something. Can you help me?

    thanks

  19. Kirk Kirk

    Anyone missing “Save Site as Template” means that you are in a Publishing template. These cannot be saved as a template without some cheating the system – and those will likely not work well with this tutorial.

  20. SmarAshe SmarAshe

    Every time I come back to creating a custom site collection template, I reference your work. Keep it up, good job

  21. Anonymous Anonymous

    Very helpful!

  22. Raj Raj

    Grear Post!
    Will this work if the Site Collection has sub-sites within ?

  23. Great article. I’m experiencing some of these issues as well..

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.