Monday, July 2, 2012

sharepoint programmatically change master page

This article i will give you the peace of code where we can change the sharepoint master page programatically.

Here is the code snippet for the same.

 SPSecurity.RunWithElevatedPrivileges(delegate()
                   {
                       using (SPSite site = properties.Feature.Parent as SPSite)
                       {
                           using (SPWeb web = site.OpenWeb())
                           {
                               //Logging feature activation status in list
                               web.AllowUnsafeUpdates = true;

                               //Changing the master page
                               web.CustomMasterUrl = "/commerce/Custom/_catalogs/masterpage/Custom.master";
                               web.MasterUrl = "/commerce/Custom/_catalogs/masterpage/Custom.master";

                               //Setting the Navigation propertiers for web
                               web.AllProperties["__IncludeSubSitesInNavigation"] = "False";
                               web.AllProperties["__IncludePagesInNavigation"] = "True";

                               web.Update();
                            }
                         }
                      }     


web.AllProperties methods will change the navigation source for the web application, This you can change according to business need.

Thank you !!



No comments:

Post a Comment