Tuesday, March 23, 2010

Customizing the Application .master page.


When we have requirements of customizing the site master page it can be done easily by editing master page using SharePoint designer as per our requirement. When we set the customized master page to a site, the site pages render with new master page but application or system pages render with Applicaton.master page. In this situation the changes we have done with custom master page mismatches with application .master page.

To bring uniformity to over all pages in site we have to modify the application master page as like custom master page.

It can be done with following way.

Step 1. Create the custom module for redirecting the control to custom Application master page from default Application. Master page at runtime.

  1. Open Visual Studio
  2. Create a New Project
  3. Choose a Class Library project (for this walkthrough I am referring to a C# class library, but the steps are the same for other types)
  4. Call it MyHttpModule
  5. Rename Class1 to MyCustomHttpModule
  6. Change the code in MyCustomHttpModule to look like this:

using System;
using System.Web;
using System.Web.UI;
using System.IO;

namespace MyHttpModule
{
public class MyCustomHttpModule: IHttpModule
{
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
}
void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null)
{
page.PreInit += new EventHandler(page_PreInit);
}
}

void page_PreInit(object sender, EventArgs e)
{
Page page = sender as Page;
if (page != null)
{
if (page.MasterPageFile != null)
{
if (page.MasterPageFile.Contains(“application.master”))
{
page.MasterPageFile = “/_layouts/MasterPages/Custom.master”;
}
}
}
}

public void Dispose()
{
}
}
}

  1. Sign the project – this is needed to add the dll to the GAC
    • Right click on the MyHttpModule project and go to the properties
    • Go to the “Signing” link on the right side
    • Click the checkbox to “Sign the assembly”
    • In the “Choose a strong name key file” dropdown, choose “
    • Enter a “Key file name”.
    • Uncheck the “Protect my key with a password” checkbox
    • Click “Ok”
  2. Build the project
  3. Place the dll in the GAC
    • If you built in Debug mode then the dll will be at: /bin/debug/MyHttpModule.dll
    • Drag the dll (do not copy and paste) into the GAC: C:\Windows\assembly

Step 2 – Register the module

  1. Find the web config for your site. If you did not specify a path when you created your web application then this is located at C:\Inetpub\wwroot\wss\VirtualDirectories\. If you did specify a different path and don’t remember where it is, the best way to figure it out is to go into IIS, find your website, go to the properties, click on the “Home Directory” tab. Your path will be in the “Local path” text box.
  2. Add this into the web.config file within the “httpModules” section

/>

Note a few things:

  • Type=,
  • PublicKeyToken – this can be found by going to the dll in the GAC (C:\Windows\assmebly), then right click and look at the properties of the dll.

Step 3 – Put a custom master page in the layouts folder

In Step 1 we told the code to look for the master page in /_layouts/MasterPages/Custom.master. Thus, we need to actually have a master page there.

  1. Copy the application.master from %Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS
  2. Create a new folder called “MasterPages” under %Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS
  3. Paste the application.master page in the newly created MasterPages folder
  4. Rename the application.master page in the Master Pages folder to Custom.master

Step 4 – Make changes to the Custom.master

Now that you have built an HttpModule to redirect to the Custom. Master page you can customize your Custom. master page however you want. But, you still have to be careful about keeping the content place holders around per the articles I have been writing in this series.

No comments: