Pages

Wednesday, September 15, 2010

How to apply custom master page into SharePoint Site

You can get information regarding master page and branding from below article.

http://dishasharepointworld.blogspot.com/2009/08/sharepoint-master-pages-and-branding_13.html

In this article, I’ll share with you how to apply custom master page into SharePoint root site and its sub sites, here I have created one feature in which I applied custom master page code into Feature Activated Event.

Example Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Collections;
namespace ApplymasterPage
{
class ApplyMasterPage : Microsoft.SharePoint.SPFeatureReceiver
{
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
if (site == null)
return;
SPWeb rootWeb = site.RootWeb;
DeactivateWeb(rootWeb);
rootWeb.Update();
foreach (SPWeb subWeb in rootWeb.Webs)
{
ProcessSubWebs(subWeb,
false);
}
}
private void DeactivateWeb(SPWeb web)
{
if (web.AllProperties.ContainsKey("OldMasterUrl"))
{
string oldMasterUrl = web.AllProperties["OldMasterUrl"].ToString();
try
{
bool fileExists = web.GetFile(oldMasterUrl).Exists;
web.MasterUrl = oldMasterUrl;
}
catch (ArgumentException)
{
web.MasterUrl = defaultMasterUrl;
}
string oldCustomUrl = web.AllProperties["OldCustomMasterUrl"].ToString();
try
{
bool fileExists = web.GetFile(oldCustomUrl).Exists;
web.CustomMasterUrl = web.AllProperties[
"OldCustomMasterUrl"].ToString();
}
catch (ArgumentException)
{
web.CustomMasterUrl = defaultMasterUrl;
}
web.AllProperties.Remove("OldMasterUrl");
web.AllProperties.Remove("OldCustomMasterUrl");
}
else
{
web.MasterUrl = defaultMasterUrl;
web.CustomMasterUrl = defaultMasterUrl;
}
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
}
const string defaultMasterUrl = "/_catalogs/masterpage/default.master";
const string customizedMasterUrl = "/_catalogs/masterpage/Custom.master";

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
if (site == null)
return;
SPWeb rootWeb = site.RootWeb;
rootWeb.AllProperties["OldMasterUrl"] = rootWeb.MasterUrl;
rootWeb.AllProperties["OldCustomMasterUrl"] = rootWeb.CustomMasterUrl;
rootWeb.MasterUrl = customizedMasterUrl;
rootWeb.CustomMasterUrl = customizedMasterUrl;
rootWeb.Update();
foreach (SPWeb subWeb in rootWeb.Webs)
{
ProcessSubWebs(subWeb,true);
}
}
private void ProcessSubWebs(SPWeb web, bool isActivation)
{
if (isActivation)
{
web.AllProperties["OldMasterUrl"] = web.MasterUrl;
web.AllProperties["OldCustomMasterUrl"] = web.CustomMasterUrl;
web.MasterUrl = web.Site.RootWeb.MasterUrl;
web.CustomMasterUrl = web.Site.RootWeb.MasterUrl;
}
else
{
DeactivateWeb(web);
}
web.Update();
foreach (SPWeb subWeb in web.Webs)
{
ProcessSubWebs(subWeb, isActivation);
}
}

}
}

No comments:

Post a Comment