I've a site in moss. I have created feature in my solution and on feature activation it will set the custom master page CustomMaster.master as a default master page and on feature deactivation it will set as a default master page default.master.
The problem I'm facing is that if I'm activation the feature at first time then it will set the CustomMaster.master as a default master page but when I'm deactivate the feature it will remove the CustomMaster.master page and module. but when I'm again activate feature at that time it will not create mo1dule and CustomMaster.master in "_catalogs/masterpage/". So, no error was given on feature activation but it will give error while I'm accessing my site as a "File Not Found" because CustomMaster.master is not created.
Code of Module.xml file
<Module Name="Master" Url="_catalogs/masterpage">
<File Path="Master\CustomMaster.master" Url="Master/CustomMaster.master" IgnoreIfAlreadyExists="TRUE" Type="GhostableInLibrary"/>
</Module>
Code of method which is call on feature activation
public void SetCustomMasterPage(SPFeatureReceiverProperties properties, string MasterPageName)
{
try
{
#region SetMasterpage
SPSite siteCollection = properties.Feature.Parent as SPSite;
if (siteCollection != null)
{
foreach (SPWeb web in siteCollection.AllWebs)
{
// Get the Site Collection root path to get the master page gallery.
string siteCollectionRoot = web.Site.RootWeb.Url;
// Set the Site Master to Custom.master
var siteMaster = new Uri(siteCollectionRoot + "/_catalogs/masterpage/Master/" + MasterPageName);
web.CustomMasterUrl = siteMaster.AbsolutePath;
web.MasterUrl = siteMaster.AbsolutePath;
// Clear the Alternate CSS
web.AlternateCssUrl = string.Empty;
// Save the changes back to the web
web.Update();
}
}
#endregion
}
catch (Exception)
{
}
}
Code of methods which are call on feature deactivation
public void SetDefaultMaster(SPFeatureReceiverProperties properties)
{
try
{
SPSite siteCollection = properties.Feature.Parent as SPSite;
if (siteCollection != null)
{
foreach (SPWeb web in siteCollection.AllWebs)
{
// Get the Site Collection root path to get the master page gallery.
string siteCollectionRoot = web.Site.RootWeb.Url;
// Set the Site Master to Custom.master
var siteMaster = new Uri(siteCollectionRoot + "/_catalogs/masterpage/default.master");
web.CustomMasterUrl = siteMaster.AbsolutePath;
web.MasterUrl = siteMaster.AbsolutePath;
// Clear the Alternate CSS
web.AlternateCssUrl = string.Empty;
// Save the changes back to the web
web.Update();
}
}
}
catch (Exception)
{
}
}
public void RemoveCustomMasterPage(SPFeatureReceiverProperties properties, string MasterPageName)
{
try
{
using (SPSite sitecollection = (SPSite)properties.Feature.Parent)
{
using (SPWeb web = sitecollection.RootWeb)
{
string WebAppRelativePath = sitecollection.ServerRelativeUrl;
if (!WebAppRelativePath.EndsWith("/"))
{
}
foreach (SPWeb site in sitecollection.AllWebs)
{
WebAppRelativePath = site.Url;
if (!WebAppRelativePath.EndsWith("/"))
{
WebAppRelativePath += "/";
}
String MasterUrl = WebAppRelativePath + "_catalogs/masterpage/Master/" + MasterPageName;
SPFile fileUrl1 = site.GetFile(MasterUrl);
if (fileUrl1 != null)
{
SPFolder folder = fileUrl1.ParentFolder;
fileUrl1.Delete();
web.Update();
//attempt to delete the folder if it is now empty
if (folder.Files.Count < 1)
folder.Delete();
}
}
}
}
}
catch (Exception)
{
}
}
Pls guide me for solve this issue.