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:
dont mind me asking this. but, where should the snippet be placed? may be this question is too dumb but can't help it.
you can create new console application and run it.
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.
or you can creat feature when it will activate it will fire above code...
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.
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.
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?
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.
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?
not tried it but we think you can change path in feature file URL tag.
@Anonymous,
can not understand your problem in just one line.
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,
@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
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.
@Anonymous, yes you required to access server.
Post a Comment