Tip – Apply Branding in WSS 3.0

Here’s a quick tip, cos I don’t have loads of time.
We’ve had lots of enquiries from our clients about Branding in WSS 3.0. If you are running MOSS 2007 then you have all of the nice “Look and Feel” settings when you go to Site Settings, allowing you to not only change the Master Page and Style Sheet, but also to push those changes down through the rest of the Portal.
Well … to put it bluntly, WSS 3.0 won’t let you do any of that.
So .. how about creating a Branding Feature?
You can write custom event handlers for Features (called Feature Receivers, using the SPFeatureReceiver class), so that you can have code that executes when a Feature is Installed, Activated, DeActivated or Uninstalled.
I won’t go into the details here, but there are some great articles about this on the web and MSDN.
So hows this for an idea?
Write yourself a Feature that applies Branding to WSS 3.0 sites (or any site for that matter!). You can make it set a new Master Page, change the Style Sheet, the Logo, set a custom “Site Theme” or even just inherit all those properties from the Parent site.
One great use (that we use quite regularly) is to create a Hidden Feature that just applies the Parent Site’s branding settings to the new site. You can then use Feature Stapling to make sure that ANY (staple to “GLOBAL”) site that gets created will be branded .. you don’t need to worry about the site definition!
As for the values used for the Branding, well you can pass these in as “Properties”. Every feature can have them, and they are specified as follows (in the Feature.xml file).
<?xml version=”1.0″ encoding=”utf-8″ ?>
<Feature xmlns=”https://schemas.microsoft.com/sharepoint/
…>
    <Properties>
      <Property Key=”NewMasterPage” Value=”/_layouts/CustomBranding/NewMasterPage.master”/>
    </Properties> 
</Feature>
So in that feature we have specified a Property called “NewMasterPage” (you can have as many of these as you like).
Then … to get that Property value out in your Feature Receiver you can use the following line of code:
string strMasterUrl = ((SPFeatureProperty)properties.Feature.Properties[“NewMasterPage”]).Value;
If the property doesn’t exist, or the value is blank then it will return an empty String (so you shouldn’t need to worry about null reference exceptions!)
Then, all you need to do is change the SPWeb property and call SPWeb.Update() to apply the changes! For example:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
// apply branding
using (SPWeb web = (SPWeb)properties.Feature.Parent)
{
   web.MasterUrl = strMasterUrl;
   web.Update();
  
}
}
Hope this gives you some good starting points! You might want to extend this further; here are some interesting things you might do:
  • Add a Property Flag to reset all sub-site branding settings. This can then be activated at the top level to reset all branding Site Collection wide.

  • Add an “inherit from Parent” Property. Then you can use SPWeb.ParentWeb to get the parent SPWeb object (and all of it’s branding settings).

  • Store the “original” branding settings (before you overwrite them) in the SPWeb.Properties hashtable. Then, add an “DeActivating” event, and put all the original properties back again! (so when you deactivate the feature, it puts the branding back in place.. a great “rollback” option!)

Good luck .. and get Branding!!!
MKeeper