Monday, July 23, 2012

Creating a Reusable Branding Solution (Deploy Custom Master Page,CSS Files and Images) in sharepoint sandbox solution.

There is one common request for customizing SharePoint sites that has been heard again
and again: “Can you make my SharePoint site look like it’s not a SharePoint site?” Whether a
company has public Internet sites or internal team sites, there is often a desire to replace the
standard SharePoint look and feel with custom colors, fonts, and images.
While different designers and developers don’t always agree on the best approach for branding
SharePoint sites, we will focus on a technique that works in either a sandbox solution or
a farm solution. That means our SharePoint solution must be designed in such a way that
it does not deploy any files inside the SharePoint root directory. Instead, our SharePoint
solution will be designed to deploy all the required branding files using template files and
Modules. We will also refrain from picking up any dependencies on SharePoint Server 2010,
which will ensure that our branding solution will work equally well on farms running either
SharePoint Foundation or SharePoint Server 2010.















Deploying a Custom Master Page
The first step in creating a branding solution is to figure out how to deploy a custom master
page to the Master Page Gallery of the top-level site. You can start by creating a Module
SharePoint Project Item Type, which is activated by a feature that is scoped at the site collection
level. Note that the Branding101 solution has a single feature named Main, which activates
at the site collection level.
The Branding101 solution contains a Module element named MasterPageGallery, which contains
a template file for a custom master page named Branding101.master. When you create
a custom master page such as Branding101.master, you will need a starting point. A popular
technique is to copy and paste the text from the standard SharePoint 2010 master page
named v4.master, which can be found at the following location.
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\GLOBAL\
v4.master

Once you have added a master page template such as Branding101.master to a Module
SharePoint Project Item Type, you must modify the elements.xml file to ensure that it is
deployed correctly to the Master Page Gallery during feature activation. The elements.xml
file of the MasterPageGallery item has been edited manually to produce the following XML.
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="MasterPageGallery"
Path="MasterPageGallery"
Url="_catalogs/masterpage" >
<File Url="Branding101.master" Type="GhostableInLibrary" >
<Property Name="UIVersion" Value="4" />
<Property Name="ContentTypeId" Value="0x010105" />
</File>
</Module>
</Elements> 

The code in the FeatureActivated event handler, which applies the branding solution.

public override void FeatureActivated(
SPFeatureReceiverProperties properties) {
SPSite siteCollection = properties.Feature.Parent as SPSite;
if (siteCollection != null) {
SPWeb topLevelSite = siteCollection.RootWeb;
// calculate relative path to site from Web Application root
string WebAppRelativePath = topLevelSite.ServerRelativeUrl;
if (!WebAppRelativePath.EndsWith("/")) {
WebAppRelativePath += "/";
}
// enumerate through each site and apply branding
foreach (SPWeb site in siteCollection.AllWebs) {
site.MasterUrl = WebAppRelativePath +
"_catalogs/masterpage/Branding101.master";
site.CustomMasterUrl = WebAppRelativePath +
"_catalogs/masterpage/Branding101.master";
site.AlternateCssUrl = WebAppRelativePath +
"Style%20Library/Branding101/Styles.css";
site.SiteLogoUrl = WebAppRelativePath +
"Style%20Library/Branding101/Images/Logo.gif";
site.UIVersion = 4;
site.Update();
}
}

In this way we can apply new branding in Sharepoint Site through sandbox solution. 

No comments:

Post a Comment