Wednesday, May 28, 2008

How to set custom master page as default master page with site definition in Moss and WSS 3.0?

Changing a default master page for our custom need is not at all good idea and creating a custom master page and use it as default master page is a headache.

Here is the way to create and use your custom master page with site definition.

Go to “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\GLOBAL” where you can find default master page (default.master).

Copy that default.master and pest it with customdefault.master or your own custom name.
(Note: while copying default.master please take care that you must not have already modified It. Use a fresh copy)

Put your custom master page at same location as default.master.

Now go to “12\TEMPLATE\GLOBAL\XML” open “ONET.xml”

At the last you will find module tag which provision “default.master” same way add your entry for custom.master.


<Modules>
<Module Name="DefaultMasterPage" List="116" Url="_catalogs/masterpage" RootWebOnly="FALSE">
<File Url=" customdefault.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" />
<File Url="default.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" />
</Module>
</Modules>



This code will automatically put your custom masterpage into the masterpage gallery when a new site is created.

Now open your onet.xml file from 12/template/SiteTemplates
Change the default masterpage to the new one.
Find <Configuration> tag
Add MasterUrl attribute like MasterUrl=”_catalogs/masterpage/customdefault.master” and if you already have this tag change it with your custom master page.

So it will look like

<Configuration ID="0" Name="YOUR NAME" MasterUrl="_catalogs/masterpage/customdefault.master">



Now the new site created with this definition have this master page as default master page. Click here for more info.

What about already existing site?

For that you need to put master page manually and change master page url for each wab
Here is the code snippet (only for setting master page url of the web):
Note: use this snippet carefully. If you run this snippet and you do not have Master page in Master page gallery for that web you will get master or page not found error.


SPSite spsite = new SPSite("Your site name");
foreach (SPWeb Myweb in spsite.AllWebs)
{
string masterurl = Myweb.ServerRelativeUrl;
if (!masterurl.EndsWith(@"/"))
masterurl = masterurl + @"/";
Myweb.MasterUrl = masterurl + "_catalogs/masterpage/customdefault.master";
Myweb.Update();
}



This kind of snippet you will find in Inside WSS 3.0 book

15 comments:

NikVad said...

dont mind me asking this. but, where should the snippet be placed? may be this question is too dumb but can't help it.

Anonymous said...

you can create new console application and run it.

Parth Patel said...

hi nikvad,
this snippet is to executed only one time.
so you can create a small stub like windows application and change master page of your existing site with this snippt.

Anonymous said...

or you can creat feature when it will activate it will fire above code...

Anonymous said...

It was quite a useful article, thanks! Just to summarize, this is what I did:

1) Created a feature to add custom master page (mycustom.master) to masterpage gallery.
<Feature Id=.... Title=.......>
<ElementManifests
<!-- Master Page -->
<ElementFile Location="Master Page\mycustom.master"/>
<!-- End of Master Page -->
</ElementManifests>
</Feature>

2) In my custom onet.xml I updated the Configuration ID=0 as follows:
<Configuration ID="0" Name="MyMainSite" MasterUrl="_catalogs/masterpage/mycustom.master">

This associated my custom master page with all the pages.

Anonymous said...

I found that setting this to an existing master page does not work. For example.

<Configuration ID="0" Name="YOUR NAME" MasterUrl="_catalogs/masterpage/blueband.master">

You will get an error.

Unless I'm doing something wrong. Please tell me i'm doing something wrong.

Anonymous said...

For existing sites, if I copy the masterpage to the masterpage gallery, that unghostes the original masterpage ... is there any way i can retrofit my existing sites so it links to the custom masterpage withough breaking ghosting?

SharePoint Kings said...

Anonymous,
refer this link http://www.sharepointkings.com/2009/06/adding-documents-in-document-library.html

use this method to add master page in master page library.

then create another feature which will had feature receiver that code will set master page as default master page(mention in this post).

this way you can achieve ghostebility.

ask us if not clear.

Anonymous said...

The previous post allows for custom masterpages to be ghosted if they reside in a feature in the features folder. However, my custom masterpage is in the 12\Template\Global folder. Can you create a feature that will allow me to put a file from the Global folder to a site's master page gallary and still have it ghosted?

SharePoint Kings said...

not tried it but we think you can change path in feature file URL tag.

SharePoint Kings said...

@Anonymous,

can not understand your problem in just one line.

Anonymous said...

Hi I'm kinda of new at sharepoint so my question would be, how is this different than using designer to right click your custom master page and pick "set as default master page"?

thanks,

SharePoint Kings said...

@Anonymous,

this is because you should not have access to production environment to access/install/and do this things and if you can then you are lucky

Anonymous said...

Do I need to be on the web server to make the copy? On my laptop, I cannot find and folders below "12". Please advise. Thank you.

SharePoint Kings said...

@Anonymous, yes you required to access server.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com