Online Training On SharePoint
                      

Monday, 21 April 2008

Dynamically Changing Master Page

In MOSS we can change the Master Page dynamically based on conditions. This helps when we need to solve the problems like:

1. Chaning the Master page for a particular browser.
2. Changing the Master Page for a particular set of users.

The following code programmatically switch to the Pocket PC friendly master page when a "Windows CE" (pocket pc) browser is detected. In the following code, the master page, pocketpc.master, changes the layout, hides some of the content and references a CSS file with fonts and layout which are better suited for the smaller form factor of a pocket pc.

protected void Page PreInit(object sender, EventArgs e) {
if(Request.Browser.Browser.ToLower.Contains("windows ce")){
this.MasterPageFile = "~/pocketpc.master";
}
else {
this.MasterPageFile = "~/MasterPage.master";
}
}

For second problem we can find the users with the help of SPUser class and then assign the Master Page.

1 comment:

Unknown said...

Hi,

If I want to change in my own page then I can write the code in PreInit method. But if I want to change the master page for all pages (event in sharepoint pages) then how will I do so?

Thanks
Sohel Rana

Related Posts with Thumbnails