Do you SharePoint?

Let's talk about it.
posts - 19, comments - 8, trackbacks - 0

Tuesday, May 26, 2009

CustomMasterUrl, MasterUrl, default.master, custom.master and ChromeMasterUrl

When changed through the API or ONET.XML, the following is true:

Site Master Page is set via SPWeb.CustomMasterUrl (Publishing Pages use this)

System Master Page is set via SPWeb.MasterUrl (Forms and Views pages use this)

Try it at home (the code below doesn't work with sub sites):

In Visual Studio, create a new C# Windows Project, Console Application. Add a reference to the Microsoft.SharePoint namespace. In the Program.cs file, paste this just inside the static void Main(string[] args):

try

{

 

    string siteURL = "http://changethistoyoursite";

  

    string newCustomMasterUrl = "/_catalogs/masterpage/???.master"; //replace ??? with your master

    string newMasterUrl = "/_catalogs/masterpage/???.master"//replace ??? with your master

 

    using (SPSite site = new SPSite(siteURL))

    {

 

        using (SPWeb web = site.OpenWeb())

        {

            string relativeUrl = (site.ServerRelativeUrl);

            if (relativeUrl == @"/")

                relativeUrl = "";

 

            Console.WriteLine("Current settings: ");

            Console.WriteLine("MasterUrl:" + web.MasterUrl);

            Console.WriteLine("CustomMasterUrl: " + web.CustomMasterUrl);

 

            Console.WriteLine();

 

            Console.WriteLine("Changing to: ");

            Console.WriteLine("MasterUrl:" + relativeUrl + newMasterUrl);

            Console.WriteLine("CustomMasterUrl: " + relativeUrl + newCustomMasterUrl);

 

            web.MasterUrl = relativeUrl + newMasterUrl;

            web.CustomMasterUrl = relativeUrl + newCustomMasterUrl;

            web.Update();

 

            Console.WriteLine("Changed.");

 

            Console.WriteLine();

            Console.WriteLine("Changed to: ");

            Console.WriteLine("MasterUrl:" + web.MasterUrl);

            Console.WriteLine("CustomMasterUrl:" + web.CustomMasterUrl);

        }

    }

}

catch (Exception ex)

{

    Console.WriteLine(ex.Message);

}

 

Console.WriteLine();

Console.WriteLine("Press ENTER to exit.");

Console.ReadKey();

 

 

Reference:

http://msdn.microsoft.com/en-us/library/bb687712.aspx

 

posted @ Tuesday, May 26, 2009 8:19 PM | Feedback (0) |

Powered by: