|
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace GlobalMasterPage {
public class FeatureReceiver : SPFeatureReceiver
{
const string defaultMasterUrl = "/_catalogs/masterpage/default.master";
const string customizedMasterUrl = "/_catalogs/masterpage/MyCustom.master";
private string defaultMaster;
private string customMaster;
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
}
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;
customMaster = (rootWeb.ServerRelativeUrl.Length == 1 ? "" : rootWeb.ServerRelativeUrl) + customizedMasterUrl;
defaultMaster = defaultMasterUrl;
rootWeb.MasterUrl = customMaster;
rootWeb.CustomMasterUrl = customMaster;
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;
if (web.ServerRelativeUrl.Length > 1)
{
customMaster = (web.Site.RootWeb.ServerRelativeUrl.Length == 1 ? "" : web.Site.RootWeb.ServerRelativeUrl) + customizedMasterUrl;
web.MasterUrl = customMaster;
web.CustomMasterUrl = customMaster;
}
}
else
{
DeactivateWeb(web);
}
web.Update();
foreach (SPWeb subWeb in web.Webs)
{
ProcessSubWebs(subWeb, isActivation);
}
}
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);
}
if (rootWeb.MasterUrl != customMaster)
{
try
{
bool fileExists = rootWeb.GetFile(customMaster).Exists;
SPFile file = rootWeb.GetFile(customMaster);
SPFolder masterPageGallery = file.ParentFolder;
}
catch (ArgumentException)
{
return;
}
}
}
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) {
defaultMaster = (web.Site.RootWeb.ServerRelativeUrl.Length == 1 ? "" : web.Site.RootWeb.ServerRelativeUrl) + defaultMasterUrl;
web.MasterUrl = defaultMaster;
}
string oldCustomUrl = web.AllProperties["OldCustomMasterUrl"].ToString();
try {
bool fileExists = web.GetFile(oldCustomUrl).Exists;
web.CustomMasterUrl = web.AllProperties["OldCustomMasterUrl"].ToString();
}
catch (ArgumentException) {
defaultMaster = (web.Site.RootWeb.ServerRelativeUrl.Length == 1 ? "" : web.Site.RootWeb.ServerRelativeUrl) + defaultMasterUrl;
web.CustomMasterUrl = defaultMaster;
}
web.AllProperties.Remove("OldMasterUrl");
web.AllProperties.Remove("OldCustomMasterUrl");
}
else {
//web.MasterUrl = defaultMasterUrl;
//web.CustomMasterUrl = defaultMasterUrl;
defaultMaster = (web.Site.RootWeb.ServerRelativeUrl.Length == 1 ? "" : web.Site.RootWeb.ServerRelativeUrl) + defaultMasterUrl;
web.MasterUrl = defaultMaster;
web.CustomMasterUrl = defaultMaster;
}
//if (web.ServerRelativeUrl.Length > 1)
//{
// defaultMaster = web.Site.RootWeb.ServerRelativeUrl + defaultMasterUrl;
// web.MasterUrl = defaultMaster;
// web.CustomMasterUrl = defaultMaster;
//}
}
}
}
|