Wednesday, 30 July 2014

Set default master page and welcome page in sharepoint 2013 using Event receiver

Share it Please
 void ChangeMasterPage(SPWeb Web, string pstrMasterURL, string pstrCustomURL)
        {

            Web.MasterUrl = pstrMasterURL;

            Web.CustomMasterUrl = pstrCustomURL;

            Web.Update();

            Web.Dispose();

        }
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            SPWeb CurrentWeb = properties.Feature.Parent as SPWeb;
            CurrentWeb.MasterUrl = CurrentWeb.Site.RootWeb.ServerRelativeUrl + "/_catalogs/masterpage/seattle.master";
            CurrentWeb.CustomMasterUrl = CurrentWeb.Site.RootWeb.ServerRelativeUrl + "/_catalogs/masterpage/Custom.master";
            SPFolder _rootFolder = CurrentWeb.RootFolder;
            _rootFolder.WelcomePage = "Pages/Home.aspx";
            _rootFolder.Update();
            CurrentWeb.Update();
            foreach (SPWeb subweb in CurrentWeb.GetSubwebsForCurrentUser())
            {

                ChangeMasterPage(subweb, CurrentWeb.MasterUrl, CurrentWeb.CustomMasterUrl);

            }

            CurrentWeb.Dispose();
        }
        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb CurrentWeb = properties.Feature.Parent as SPWeb;

            CurrentWeb.MasterUrl = CurrentWeb.Site.RootWeb.ServerRelativeUrl + "/_catalogs/masterpage/seattle.master";

            CurrentWeb.CustomMasterUrl = CurrentWeb.Site.RootWeb.ServerRelativeUrl + "/_catalogs/masterpage/seattle.master";
            SPFolder _rootFolder = CurrentWeb.RootFolder;
            _rootFolder.WelcomePage = "Pages/Default.aspx";
            _rootFolder.Update();
            CurrentWeb.Update();
        }

No comments:

Post a Comment

Followers

Follow The Author