Apply Master Page to Site Programmatically in SharePoint

Leave a Comment

Hello folks,

In following Example we will see how to apply master page to the sub sites in SharePoint.

        /// <summary>
        /// Used to Apply the Master Page to the SubSites
        /// </summary>
        /// <param name="oSite">SPSite - Site Object</param>
        /// <param name="oWeb">SPWeb - Web Object</param>
        /// <param name="obj">SiteType</param>
        private void ApplyMasterPage(SPSite oSite, SPWeb oWeb, Utilities obj)
        {
            try
            {
                string SystemMasterPath ="_catalogs/masterpage/xx.master";
                //// Set the new web’s top link bar to inherit links from its parent web.
                oWeb.Navigation.UseShared = true;

                Hashtable hash = oWeb.AllProperties;

                hash["__InheritsMasterUrl"] = "True";

                oWeb.MasterUrl = oSite.RootWeb.ServerRelativeUrl + SystemMasterPath;

                hash["__InheritsCustomMasterUrl"] = "True";

               //// setting the Master Page from Root Site Master Pages and Page Layouts
                    oWeb.CustomMasterUrl = oSite.RootWeb.ServerRelativeUrl
                                                                                                                                                + “_catalogs/masterpage/xxx.master”;                   }
            catch (Exception ex)
            {
                //Exception Handling
            }
        }

Related Post

0 comments:

Post a Comment