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.
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.
1. Create a WSP Project in Visual Studio:
2. Right click the Project and choose "Add > New Item" to the Project
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"
<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="http://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="http://schemas.microsoft.com/sharepoint/" >
<ElementManifests>
<ElementManifest Location="elements.xml" />
</ElementManifests>
</Feature>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!--
staple all site defitions to Branding -->
<FeatureSiteTemplateAssociation
Id="4b4b1479-2d1e-4e4d-a04a-a5f34c96309b"
TemplateName="GLOBAL" />
</Elements>
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.");
}
}
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