Tuesday, April 2, 2013

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:
SharePoint 2007 Branding Solution with WSP Builder
2. Right click the Project and choose "Add > New Item" to the Project
Create Branding Feature SharePoint

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.

SharePoint Branding Scope

5. Now, add the feature for FeatureStaple: Add New Item, and this time choose "Blank Feature"

SharePoint Branding Feature Stapler

6. Set the scope for FeatureStapler as "WebApplication"

SharePoint Branding Stapler Scope

7.Add your own Master Page,  CSS, Site Map files, Custom Navigation Provider, etc. to the solution.
Overall Project Structure:

branding moss 2007

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="http://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" ?>
    <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"
  <ElementManifests>
    <ElementManifest Location="elements.xml" />
  </ElementManifests>
</Feature>


Element.xml on Portal.Branding.FeatureStapler


   <!-- 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.

Important:
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!

No comments:

Post a Comment