31/03/2008

Ensuring that your custom master file is loaded on each new site created

The following code ensures that when a site is provisioned it uses your master file.

Once you have compiled it you will need to register the http module in the web.config file.

e.g.

<add name="ensuremaster" type="ensuremaster.ensuremaster,ensuremaster, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e2afe595d5cd71e3" />

using System;

using System.Web;

using System.Web.UI;

using System.IO;

namespace ensuremaster

{

public class ensuremaster : 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 default.master files as those are the offenders

page.MasterPageFile = "/_catalogs/masterpage/samplests.master";

}

}

}

public void Dispose()

{

}

}

}

No comments:

Post a Comment