Do you SharePoint?

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

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

 

Print | posted on Tuesday, May 26, 2009 8:19 PM |

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 2 and 4 and type the answer here:

Powered by: