1

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.

3
  • Is it on purpose that you create the masterpage in a subfolder of the gallary called Master? Is it working the first time you're activating the feature? How do you remove the file? Apr 26, 2014 at 13:47
  • I'm calling this method on feature activation public void SetCustomMasterPage(SPFeatureReceiverProperties properties, string MasterPageName) { #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.Absolut Apr 29, 2014 at 6:18
  • I would advice you to make an edit on your post with that last information, for all users to be able to see it and for giving it proper markup Apr 29, 2014 at 6:25

1 Answer 1

0

Try separate the activation logic in two Features. One for module and second for setting Custom masterpage. If this is acceptable then all you need is set dependency check on second Feature that sets Custom masterpage as the one that should already be activated. http://msdn.microsoft.com/en-us/library/ee231535.aspx. Let us know if this is the case.

2
  • My solution is in Visual studio 2008. and the link which you have given is of Visual studio 2010/2012/2013 then also I've tried to create different features for set custom master as default and remove master custom master page features but still it is not working Apr 29, 2014 at 6:20
  • my both features are independent. they are not dependent on each other Apr 29, 2014 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.