Everything SharePoint/Silverlight/WP7

My continuous learning of SharePoint, Silverlight, WP7 Development, Office, VSTO, C#…

Customize application.master

Posted by Steve Pietrek on January 29, 2008

I have been doing some research on customizing application.master. I found a good KB on Microsoft’s web site listing two ways to customize application.master.

  1. Customize the files in the Layouts folder (recommended)
  2. Create a custom Layouts folder

The two methods above work well.

I have been messing around with an application development method using a feature, HttpModule, and SPWebConfigModification. The premise of the feature is for every page, in the PreInit event, I check to see if the page’s current MasterPageFile is application.master. If it is, I set the MasterPageFile to my custom application.master. I plan on blogging on this method soon.

Advertisement

One Response to “Customize application.master”

  1. I have code samples for you!

    namespace CustomDesign
    {
    public class CustomPageInit : IHttpModule
    {
    public void Init(HttpApplication context)
    {
    context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
    }

    void context_PreRequestHandlerExecute(object sender, EventArgs e)
    {
    HttpApplication httpApp = sender as HttpApplication;

    if (httpApp != null)
    {
    Page page = httpApp.Context.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.MasterPageFile != null)
    {
    if (page.MasterPageFile.ToLower().Contains("application.master")) {

    SPWeb objWeb = SPContext.Current.Web;

    if (objWeb.MasterUrl.Contains("default.custom.master"))
    {
    objWeb.AllowUnsafeUpdates = true;
    objWeb.Update();
    string strTmp = page.MasterPageFile.ToLower().Replace("application.master", "application.custom.master");
    page.MasterPageFile = strTmp;
    }
    }
    }
    }

    public void Dispose()
    {
    }
    }
    }

    In the web.config, append that line into the httpmodule section

Leave a Reply

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

Gravatar
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 )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.