PANVEGA’s Blog

DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

Add a Custom Application.master by using the HttpHandler

Posted by PANVEGA on September 3, 2009

In this post I am going to show you how to apply a custom Application.master to your ShareP0int application by using the IHttpModule. My approach was to create an “HttpHandler” that simply would override the “Page_PreInit” method and change the master page.

Of course you have to create a solution in order to deploy your CustomApplication.master to the 12 Hive folder (e.g. LAYOUTS\CustomMaster\CustomApplication.master). The path to your master page in your 12 Hive and  in the code below should be correct.

page.MasterPageFile = “/_layouts/CustomMaster/CustomApplication.master”;

After you activated the feature and you want to load a page setting for instance, Sharepoint should now apply your custom master.

Background:

Create HTTP module that injects a custom master page on a per-request basis – This approach currently is the only available  option considering the fact that you neither have to touch any of the files in the 12 Hive and that you can configure different master pages for each web application.

How Does It Work?

HTTP handlers (not HTTP modules) have access to the application context, including the requesting user’s identity (if known), application state, and session information. When an HTTP handler is requested, ASP.NET calls the ProcessRequest method on the appropriate handler. The handler’s ProcessRequest method creates a response, which is sent back to the requesting browser. As with any page request, the response goes through any HTTP modules that have subscribed to events that occur after the handler has run. What we’re going to do is to subscribe our own module to such events.

here is a good example

Code example:

public class ApplicationMasterModule : 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)
{
// Is there a master page defined?
if (page.MasterPageFile != null)
{
// only change the application.master files as those are the offenders
if (page.MasterPageFile.Contains(“application.master”))
{
page.MasterPageFile = “/_layouts/CustomMaster/CustomApplication.master”;

} } } }
}

Sign and compile the assembly and copy it into the GAC. Next we need to register the module with the desired web applications.

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\<port>. 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

<add name=”CustomApplicationHttpModule” type=”Receiver_Application_Master.ApplicationMasterModule, Receiver_Application_Master, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXXXXXXXXX” />

read my previous post how to register your module by creating a feature receiver class.

https://panvega.wordpress.com/2009/09/02/using-spwebconfigmodification-within-a-feature-receiver

Keep always in mind when modifying a application.master

  • Don’t remove content place holders. The pages that implement the application master page assume these content place holders are there. So, any content place holder from application.master must be in your custom application master page. If you are sure you have a content place holder that is not needed, then don’t remove it – just wrap it in a tag and turn the visible property to false.
  • You can’t have a search control on the application master pages. I am not sure of the reason for this, but you can’t have the out of the box search control on the application master pages.
  • Some of the application pages are actually formatted incorrectly. Thus, when you build a custom application master page you could inadvertently mess up an out of the box application page. This can happen even if your application master page is formatted perfectly.
  • Do not use this method if your install has the SSP on the same server as the main SharePoint application. There has been reported issues of the SSP having issues with this approach because the system master pages they use have a different setup.

Good Luck everbody!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: