Wednesday, January 18, 2012

Custom Web Templates and Activating Features

Lately I was messing around with Custom Site and Web Templates with Office 365 and made a few discoveries that I figure I share. Some of it is old, some of it I had to piece together.

I am building a solution in Office 365 using SharePoint Online. I have a site collection with publishing turned on. I created a custom site template and I need to make sure that site (web level) publishing feature is turned on.

Publishing Feature Disables Creating Site Templates
For some reason, when you turn on the publishing feature on a site, the “Save site as a template” is removed from Site Settings. Now I have found some blog posting where people go down into the SharePoint install files and make some changes to fix that. First, probably not the best course of action if you want to make sure you have supportable upgrades (and someone actually documents the change <g>). Second, since I am working with Office 365 that is not even an option, so I need to find a solution that does it the right way.

Another thing I saw people suggest is go to the Save as Template page by typing in “_layouts/savetmpl.aspx”. Do not waste your time with that approach either, it will not work correctly.

My only option is to create my custom site initially without the publishing feature turned on. Then build some solutions to turn on publishing for the site automatically and ensure that the masterpages are set appropriately. The solutions I looked into are:

1. Feature Stapling
2. Modifying the Web Template
3. WebProvisioned Event Handler

Custom Web Template Feature Stapling
The first solution that I investigated was the most obvious; create a Feature Stapler that would activate the site publishing feature.

The first thing I ran into was finding the actual name of the template. Traditionally the name of the site template is usually something like STS#0. However since this is a custom site template where would I get the site template name from? I found some scripts that would help me get the template name. Since I was doing the development on my local machine it was easy for me to run some PowerShell on my development box to test this all out (I did find similar code to run API code against Office 365).

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="">
  <FeatureSiteTemplateAssociation Id="94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb" TemplateName="{04FB0801-1DFA-41ED-9B87-06D0FF088F01}#MyCommunityTemplate" />

I create a Feature Stapling feature that looked like this. As you can see there is a GUID#template name format.

Let me save you some though – this is not supported and did not work. Feature Stapling can only be used against the site templates that are OOB the box or deployed down into the hive. You cannot apply do Feature Stapling to a site template created off SharePoint web site.
Modifying the Web Template

The next thing I tried, which is a recommended approach, is to modify the web template. This is a really easy thing to do now with Visual Studio 2010.
In Office 365 I created a web template the way I needed but without publishing turned on. I then went into site settings and created web template like usual. I then went to the Site Collection Solution Gallery, downloaded the WSP, and opened it in Visual Studio 2010 (using an Import SharePoint Solution Package project).
In the project, there will be a module called “Web templates” and you will find a file called “ONet.xml”. Open this file and you will see <SiteFeatures> and <WebFeatures>.
<SiteFeatures> correspond to the site collection and the following features should already be in the web template because you will have publishing turned on at the site collection level.

<!--PublishingPrerequisites Feature-->
<Feature ID="{a392da98-270b-4e85-9769-04c0fde267aa}" Name="FeatureDefinition/a392da98-270b-4e85-9769-04c0fde267aa" />
<!--PublishingResources Feature-->
<Feature ID="{aebc918d-b20f-4a11-a1db-9ed84d79c87e}" Name="FeatureDefinition/aebc918d-b20f-4a11-a1db-9ed84d79c87e" />
<!--PublishingLayouts Feature-->
<Feature ID="{d3f51be2-38a8-4e44-ba84-940d35be1566}" Name="FeatureDefinition/d3f51be2-38a8-4e44-ba84-940d35be1566" />
<!--PublishingSite Feature-->
<Feature ID="{f6924d36-2fa8-4f0b-b16d-06b7250180fa}" Name="FeatureDefinition/f6924d36-2fa8-4f0b-b16d-06b7250180fa" />

Why are there four when you only turn on one publishing feature for the site collection through the SharePoint UI? The one with f6924d36-2fa8-4f0b-b16d-06b7250180fa is actually the main one, but the other three are supporting, hidden ones. There is not action for you, just brought this up because I thought is it interesting.

Now this is what you do need to do. Go to the <WebFeatures> and add the following two features.

Add the Publishing Web after the MetaDataNav:
<!--MetaDataNav Feature-->
<Feature ID="{7201d6a4-a5d3-49a1-8c19-19c4bac6e668}" Name="FeatureDefinition/7201d6a4-a5d3-49a1-8c19-19c4bac6e668" SourceVersion="" />
<!--PublishingWeb Feature-->
<Feature ID="{94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb}" Name="FeatureDefinition/94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb" SourceVersion="" />

Add the Publishing Feature after the GridList:
<!--GridList Feature-->
<Feature ID="{00bfea71-3a1d-41d3-a0ee-651d11570120}" Name="FeatureDefinition/00bfea71-3a1d-41d3-a0ee-651d11570120" SourceVersion="" />
<!--Publishing Feature-->
<Feature ID="{22a9ef51-737b-4ff2-9346-694633fe4416}" Name="FeatureDefinition/22a9ef51-737b-4ff2-9346-694633fe4416" SourceVersion="" />

This worked great and was really clean.

The next thing I needed do was make sure the master pages are set appropriately. Even though I turned on publishing, it does not mean that the mast pages will be set correctly. The solution is again very simple.

I created a new Feature in Visual Studio 2010 to the web template solution I already had open and set the scope for Web. I then added an event handler to the new Feature. Then in the FeatureActivated event handler I added the following code:
using (SPWeb web = (SPWeb)properties.Feature.Parent)
    web.MasterUrl = web.ParentWeb.MasterUrl;
    web.AllProperties["__InheritsMasterUrl"] = "True";
    web.CustomMasterUrl = web.ParentWeb.CustomMasterUrl;
    web.AllProperties["__InheritsCustomMasterUrl"] = "True";

Then I selected the new Feature, clicked on the manifest and copied the GUID for the Feature ID. I then subsequently added that Feature ID into the bottom of the <WebFeatures> section in the ONet.xml file.
Now when this custom web template is provisioned, this code will be executed to set the master page appropriately.
Web Provisioned Event
Another totally different option to consider is use the new SharePoint 2010 WebProvisioned Event Handler. This event handler is available in the SharePoint Sandbox and can run in Office 365. This solution is very simple:
  • Create a new SharePoint 2010 solution in Visual Studio.  
  • Add an Event Handler. 
  • Then add the following code.
public override void WebProvisioned(SPWebEventProperties properties)

    using (SPWeb web = (SPWeb)properties.Web)
        Guid sitePublishing = new Guid("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb");

        web.AllowUnsafeUpdates = true;

        web.Features.Add(sitePublishing, true);
        web.MasterUrl = web.ParentWeb.MasterUrl;
        web.AllProperties["__InheritsMasterUrl"] = "True";
        web.CustomMasterUrl = web.ParentWeb.CustomMasterUrl;
        web.AllProperties["__InheritsCustomMasterUrl"] = "True";


There are some advantages to this solution. First it is really simple. Second this code will be executed for ANY site that is provisioned. So if you need to always make sure that publishing is turned on for any site that is provisioned and that the master pages are always the same, this may actually be a better solution. Otherwise you will have to do what I previously described for every web template.


This was a really interesting little exercise that I went through and though it would be interesting to share. Hopefully you will see that there are options based on the business requirements you want to support. This is by no means the only options you have available either.


Things that I found along the way:

No comments: