Monday 10 November 2014

SharePoint Apply Branding using Feature


Apply Branding using Feature

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
Add a class
public class ApplyBranding : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = (SPWeb)properties.Feature.Parent;
using (SPSite site = new SPSite(web.Url))
{
using (SPWeb rootWeb = site.RootWeb)
{
string serverRelativeUrl = rootWeb.ServerRelativeUrl;
string masterUrl = getPropertyFrom(properties, "MasterUrl");
string customMasterUrl = getPropertyFrom(properties, "CustomMasterUrl");
string alternateCssUrl = getPropertyFrom(properties, "AlternateCssUrl");
string siteLogoUrl = getPropertyFrom(properties, "SiteLogoUrl");
string theme = getPropertyFrom(properties, "Theme");
bool isMySite = getPropertyFrom(properties, "IsMySite").Equals("TRUE", StringComparison.InvariantCultureIgnoreCase);
if (serverRelativeUrl != "/" && !isMySite)
{
masterUrl = mergeUrls(serverRelativeUrl, masterUrl);
customMasterUrl = mergeUrls(serverRelativeUrl, customMasterUrl);
alternateCssUrl = mergeUrls(serverRelativeUrl, alternateCssUrl);
}
saveOldBranding(web);
applyBranding(web, masterUrl, customMasterUrl, alternateCssUrl, siteLogoUrl, theme, isMySite);
}
}
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWeb web = (SPWeb)properties.Feature.Parent;
bool isMySite = getPropertyFrom(properties, "IsMySite").Equals("TRUE", StringComparison.InvariantCultureIgnoreCase);
if (!isMySite)
{
resetBranding(web);
}
else
{
string masterUrl = "default.master";
string customMasterUrl = "default.master";
string alternateCssUrl = "";
string siteLogoUrl = "";
string theme = "";
web.Properties.Update();
applyBranding(web, masterUrl, customMasterUrl, alternateCssUrl, siteLogoUrl, theme, isMySite);
}
}
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{ }
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
SPWeb web = (SPWeb)properties.Feature.Parent;
string masterUrl = removeFromwebProperties(web, "BrightstarrBrandingMasterPageUrl");
string customMasterUrl = removeFromwebProperties(web, "BrightstarrBrandingCustomMasterUrl");
string alternateCssUrl = removeFromwebProperties(web, "BrightstarrBrandingAlternateCssUrl");
string siteLogoUrl = removeFromwebProperties(web, "BrightstarrBrandingSiteLogoUrl");
string theme = removeFromwebProperties(web, "BrightstarrBrandingTheme");
web.Properties.Update();
web.Update();
}
private string getPropertyFrom(SPFeatureReceiverProperties properties, string key)
{
string propertyValue = string.Empty;
try
{
propertyValue = properties.Feature.Definition.Properties[key].Value;
}
catch
{ }
return propertyValue;
}
private void saveOldBranding(SPWeb web)
{
saveToWebProperties(web, "BrightstarrBrandingMasterPageUrl", web.MasterUrl);
saveToWebProperties(web, "BrightstarrBrandingCustomMasterUrl", web.CustomMasterUrl);
saveToWebProperties(web, "BrightstarrBrandingAlternateCssUrl", web.AlternateCssUrl);
saveToWebProperties(web, "BrightstarrBrandingSiteLogoUrl", web.SiteLogoUrl);
saveToWebProperties(web, "BrightstarrBrandingTheme", web.Theme);
web.Properties.Update();
}
private void saveToWebProperties(SPWeb web, string key, string value)
{
saveToWebProperties(web, key, value, false);
}
private void saveToWebProperties(SPWeb web, string key, string value, bool update)
{
if (!web.Properties.ContainsKey(key))
web.Properties.Add(key, value);
if (update)
web.Properties.Update();
}
private void applyBranding(SPWeb web, string masterPageUrl, string customMasterUrl, string alternateCssUrl, string siteLogoUrl, string theme)
{
applyBranding(web, masterPageUrl, customMasterUrl, alternateCssUrl, siteLogoUrl, theme, false);
}
private void
applyBranding(SPWeb web, string masterPageUrl, string customMasterUrl, string alternateCssUrl, string siteLogoUrl, string theme, bool isMySite)
{
if (!string.IsNullOrEmpty(masterPageUrl))
{
if (isMySite)
{
if (web.MasterUrl.Contains("default.master"))
{
web.MasterUrl = web.MasterUrl.Replace("default.master", masterPageUrl);
}
else
{
web.MasterUrl = web.MasterUrl.Replace(masterPageUrl, "default.master");
}
}
else
web.MasterUrl = masterPageUrl;
}
if (!string.IsNullOrEmpty(customMasterUrl))
{
if (isMySite)
{
if (web.CustomMasterUrl.Contains("default.master"))
{
web.CustomMasterUrl = web.CustomMasterUrl.Replace("default.master", customMasterUrl);
}
else
{
web.CustomMasterUrl = web.CustomMasterUrl.Replace(customMasterUrl, "default.master");
}
}
else
web.CustomMasterUrl = customMasterUrl;
}
web.AlternateCssUrl = alternateCssUrl;
web.SiteLogoUrl = siteLogoUrl;
web.ApplyTheme(theme);
web.Update();
}
private void resetBranding(SPWeb web)
{
string masterUrl = fetchFromwebProperties(web, "BrightstarrBrandingMasterPageUrl");
string customMasterUrl = fetchFromwebProperties(web, "BrightstarrBrandingCustomMasterUrl");
string alternateCssUrl = fetchFromwebProperties(web, "BrightstarrBrandingAlternateCssUrl");
string siteLogoUrl = fetchFromwebProperties(web, "BrightstarrBrandingSiteLogoUrl");
string theme = fetchFromwebProperties(web, "BrightstarrBrandingTheme");
applyBranding(web, masterUrl, customMasterUrl, alternateCssUrl, siteLogoUrl, theme);
}
private string removeFromwebProperties(SPWeb web, string key)
{
return removeFromwebProperties(web, key, false);
}
private string removeFromwebProperties(SPWeb web, string key, bool update)
{
string returnValue = string.Empty;
if (web.Properties.ContainsKey(key))
{
returnValue = web.Properties[key];
web.Properties.Remove(key);
}
if (update)
web.Update();
return returnValue;
}
private string fetchFromwebProperties(SPWeb web, string key)
{
string returnValue = string.Empty;
if (web.Properties.ContainsKey(key))
{
returnValue = web.Properties[key];
}
return returnValue;
}
private string mergeUrls(string serverUrl, string relativeUrl)
{
string returnUrl = serverUrl;
if (!serverUrl.EndsWith("/"))
returnUrl = serverUrl + "/";
if (relativeUrl.Length > 1)
{
if (relativeUrl.StartsWith("/"))
relativeUrl = relativeUrl.Substring(1);
returnUrl = returnUrl + relativeUrl;
}
return returnUrl;
}
}