Building Branding Solution with Feature Stapler for SharePoint 2007
Feature Staplers are the best way to build branding solutions in SharePoint. Once deployed branding will be automatically applied based on Site definition.
Overall Steps: Create a WSP Project and add 2 features in it.
- One for actual Branding with Feature handler code – Applies Branding on activated
- and another one for Stapling the feature – Binds the feature with site with Site definitions.
Lets get started.
1. Create a WSP Project in Visual Studio:
2. Right click the Project and choose “Add > New Item” to the Project
3. Under “WSPBuilder” choose “Feature with Receiver”, Enter the Name for the feature.
4. Set the scope for the Feature as “Web” meaning sub-site.
5. Now, add the feature for FeatureStaple: Add New Item, and this time choose “Blank Feature”
6. Set the scope for FeatureStapler as “WebApplication”
7.Add your own Master Page, CSS, Site Map files, Custom Navigation Provider, etc. to the solution.
Overall Project Structure:
Update the Feature.xml with additional entries like Title, Icon, as below code.
Feature.xml on Portal.Branding:
<?xml version="1.0" encoding="utf-8"?>
<Feature Id="4b4b1479-2d1e-4e4d-a04a-a5f34c96309b"
Title=" Portal Branding"
Description="Branding Solution for Portal"
Version="12.0.0.0"
Hidden="FALSE"
Scope="Web"
ImageUrl="bpCorp\feature-icon.png"
DefaultResourceFile="core"
ReceiverAssembly="Portal.Branding, Version=1.0.0.0, Culture=neutral, PublicKeyToken=bb1f1034da9a39ca"
ReceiverClass="Portal.Branding.Branding"
xmlns="https://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="elements.xml"/>
</ElementManifests>
<Properties>
<Property Key="SiteLogoUrl" Value="/_layouts/images/BPCorp/bp-logo.png"/>
</Properties>
</Feature>
Element.xml on Portal.Branding
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
<Module Name="MasterPagesModule" List="116" Url="_catalogs/masterpage" Path="MasterPage">
<File Url="BPCorp.master" Type="GhostableInLibrary" />
</Module>
</Elements>
Feature.xml on Portal.Branding.FeatureStapler
<Feature
Id="247385FE-1A70-4cfc-980C-5517B7609D58"
Title=" Branding Stapler"
Scope="WebApplication"
xmlns="https://schemas.microsoft.com/sharepoint/" >
<ElementManifests>
<ElementManifest Location="elements.xml" />
</ElementManifests>
</Feature>
Element.xml on Portal.Branding.FeatureStapler
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
<!-- staple all site defitions to Branding -->
<FeatureSiteTemplateAssociation
Id="4b4b1479-2d1e-4e4d-a04a-a5f34c96309b"
TemplateName="GLOBAL" />
</Elements>
and Finally, our feature activation code goes here:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Diagnostics;
namespace Portal.Branding
{
class Branding : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
//Set the Customized Master page as Default & Cutom Master page
SPWeb rootWeb = properties.Feature.Parent as SPWeb;
// Calculate relative path to site from Web Application root.
string WebAppRelativePath = rootWeb.ServerRelativeUrl;
if (!WebAppRelativePath.EndsWith("/"))
{
WebAppRelativePath += "/";
}
rootWeb.MasterUrl = WebAppRelativePath + "_catalogs/masterpage/BPCorp.master";
rootWeb.CustomMasterUrl = WebAppRelativePath + "_catalogs/masterpage/BPCorp.master";
rootWeb.AlternateCssUrl = WebAppRelativePath + "_layouts/1033/styles/BPCorp.css";
//Set the Logo
rootWeb.SiteLogoUrl = properties.Feature.Properties["SiteLogoUrl"].Value;
rootWeb.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
//Revert the Master page to "default.master"
SPWeb rootWeb = properties.Feature.Parent as SPWeb;
// Calculate relative path to site from Web Application root.
string WebAppRelativePath = rootWeb.ServerRelativeUrl;
if (!WebAppRelativePath.EndsWith("/"))
{
WebAppRelativePath += "/";
}
rootWeb.MasterUrl = WebAppRelativePath + "_catalogs/masterpage/default.master";
rootWeb.CustomMasterUrl = WebAppRelativePath + "_catalogs/masterpage/default.master";
rootWeb.AlternateCssUrl = "";
//Set the Logo
rootWeb.SiteLogoUrl = "/_layouts/images/titlegraphic.gif";
rootWeb.Update();
}
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
//throw new Exception("The method or operation is not implemented.");
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
//throw new Exception("The method or operation is not implemented.");
}
}
}
Now, our solution is ready! Build the WSP, Install the solution, Activate the FeatureStapler Feature at web application level. Activate the Branding Feature for existing sites.
Since we used Feature Stapling, new sites will get this branding automatically on their create event. How about existing sites? Well, for existing sites, we need to activate this feature manually in order to get the branding applied!
MSDN post on building Feature for Master page:https://msdn.microsoft.com/en-us/library/ms441170.aspx
how to keep a static notification across all the site pages in 2007? We want to get a message displayed on the each page of the SharePoint site.
This should help you: How to Add a Notification Message in Top Banner of the SharePoint Site
How do you handle Meeting Workspaces; they use a diff master
As meeting workspace is a different site template, change the feature stapler’s template association. E.g. Instead of “Global” use: MPS#0 for basic meeting workspace.
Hi Salaudeen,
I followed your guide and its working fine for using a custom Master Page with my sites but I am wondering what to add to the Feature Event Receiver to add a Web Part to all sites that are created…do you have any idea?
Thank You for any help
You can Create a Feature for Web Part and use the Feature stapler to bind it. For existing sites, Just activate the feature using STSADM.
Hi Salaudeen
thanx alot you saved my time.