Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11508

Custom Master page set as a default on feature activation in Moss

$
0
0

I've a site in moss. I have created feature in my solution and on feature activation it will set the custom master pageCustomMaster.master as a default master page and on feature deactivation it will set as a default master pagedefault.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 theCustomMaster.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" becauseCustomMaster.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.


Viewing all articles
Browse latest Browse all 11508

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>