Friday, March 8, 2013

SetMasterAndWelcomePageOn feature activates Programatically


[Code]

public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {            try            {               SPSecurity.RunWithElevatedPrivileges(delegate                {                    using (var web = properties.Feature.Parent as SPWeb)                    {                        if (web == null) return;
                        web.ValidateFormDigest();                        SetMasterPage(web);                        SetDefaultHomePage(web);                      }                });            }            catch (Exception ex)            {            }            finally            {            }        }
        private static void SetDefaultHomePage(SPWeb varoWeb)        {            try            {                    varoWeb.AllowUnsafeUpdates = true;                    SPFolder folder = varoWeb.RootFolder;                    folder.WelcomePage = "Pages/home.aspx";                    folder.Update();                    varoWeb.AllowUnsafeUpdates = false;            }            catch (Exception e)            {    throw new SPException("Error when trying to Set DefaultPage. Error: " + e.Message);            }        }  
        private void SetMasterPage(SPWeb varWeb)        {            try            {                var strMasterUrl = "/_catalogs/masterpage/myMasterPage.master";
                varWeb.AllowUnsafeUpdates = true;                varWeb.MasterUrl = strMasterUrl;                varWeb.CustomMasterUrl = strMasterUrl;                varWeb.Update();                varWeb.AllowUnsafeUpdates = false;            }            catch (Exception e)            {                throw new SPException("Error when trying to Set MasterPage. Error: " + e.Message);            }        }

No comments:

Post a Comment