Custom Application MasterPage in Sharepoint 2007

No.of Views2176
Bookmarked0 times
Downloads 
Votes0
By  amalhashim   On  05 Sep 2010 09:09:06
Tag : SharePoint , Enterprise Content Management
Recently I have involved in branding one of our clients sharepoint portal. As part of the branding process we needed to modify the application.master file, so even the layout pages will have the same look and feel. As part of the investigation, we come up with the best approach for doing this.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Recently I have involved in branding one of our clients sharepoint portal. As part of the branding process we needed to modify the application.master file, so even the layout pages  will have the same look and feel. As part of the investigation, we come up with the best approach for doing this.

1. Create a custom master page, with all the placeholders which are available in the original application.master file. Its better to take a copy of the application.master file and add the styles/images etc as you wish

2. Create a custom HttpModule which will set the new custom master page if the current page is having the master page “application.master”. We preferred HttpModule on top of HttpHandler because HttpModule can be deployed only to the web applications we wants, while HttpHandler will hit across all web applications.

Below is the code i have created for the http module.

using System;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint;

namespace Company.HttpModules
{/// <summary>/// All the layout pages will be using application.master/// inorder to make the application consistent. we have created a custom application master page/// this module will check whether the current request is having the master page as application.master/// then change it to our custom page/// </summary>public class ApplicationMasterModule : IHttpModule
    {public void Init(HttpApplication context)
        {if (context == null)throw new ArgumentNullException("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"))
                    {using (SPSite connectSite = new SPSite(SPContext.Current.Site.Url))
                        {using (SPWeb currentWeb = connectSite.RootWeb)
                            {string url = currentWeb.ServerRelativeUrl + "/_catalogs/masterpage/our_custom_app.master";
                                page.MasterPageFile = url;
                            }
                        }
                    }
                }
            }
        }public void Dispose()
        {
        }
    }
}

 Now comes the important aspect of deployment. Its better to create the master page as well as http module to be deployed as a feature. See the feature.xml

<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="b1531032-19b5-4fb1-87a6-6892b280c90d"
          Title="Custom Application Master Page"
          Description="Custom Application Master Page"
          Version="12.0.0.0"
          Hidden="FALSE"
          Scope="Site"
          DefaultResourceFile="core"
          ReceiverAssembly="Company.MasterPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6c27a5016ad8e167"
          ReceiverClass="Company.MasterPages.CustomAppMasterPage"          
          xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="elements.xml"/>
      <ElementFile Location="our_custom_app.master" />
  </ElementManifests>
</Feature>

and elements.xml file

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Module Name="CustomApplicationMasterPage"
            Url="_catalogs/masterpage"
            Path=""
            RootWebOnly="False">
        <File Url="our_custom_app.master"
              Type="GhostableInLibrary" IgnoreIfAlreadyExists="FALSE">
            <Property Name="ContentType"
                      Value="$Resources:cmscore,contenttype_-9masterpage_name;" />
            <Property Name="PublishingPreviewImage"
                      Value="" />
            <Property Name="Description"
                      Value="Custom Application Master Page"/>
        </File>            
    </Module>
</Elements>

See the Feature Receiver assembly which will add the web.config entries so the HttpModule will work without manually modifying the web configuration file

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Globalization;
using System.Diagnostics;

namespace Company.UIUpdates
{/// <summary>/// On Feature Activation => Add HttpModule Entry in Web.Config file/// On Feature DeActivation => Remove the HttpModule Entry from Web.Config file/// </summary>class CustomAppMasterPage : SPFeatureReceiver
    {static SPWebConfigModification CreateModification(string Name, string XPath, string Value)
        {
            SPWebConfigModification modification = new SPWebConfigModification(Name, XPath);
            modification.Owner = "Custom Application Master Page.wsp";
            modification.Sequence = 0;
            modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            modification.Value = Value;return modification; 
        }public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {try{using (SPSite site = properties.Feature.Parent as SPSite)
                {
                    SPWebApplication webApp = site.WebApplication;string name = "add[@name='ApplicationMasterModule']";string path = "configuration/system.web/httpModules";string value = @"<add name=""ApplicationMasterModule"" type=""Company.HttpModules.ApplicationMasterModule,Company.HttpModules, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f2391931b556f037"" />";if (webApp != null)
                    {
                        SPWebConfigModification modification = CreateModification(name, path, value);
                        webApp.WebConfigModifications.Add(modification);
                        webApp.Update();
                        SPWebService.ContentService.ApplyWebConfigModifications();
                        SPWebService.ContentService.WebApplications[webApp.Id].Update();//Applies the web config settings in all the web application in the farmSPWebService.ContentService.WebApplications[webApp.Id].WebService.ApplyWebConfigModifications();
                    }
                }
            }catch (Exception ex)
            {
                WriteMessageToEventLog(ex.ToString());
            }        
        }static private void WriteMessageToEventLog(string message)
        {string EVENT_SOURCE = "CustomAppMasterPage Feature Receiver";
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {if (!EventLog.SourceExists(EVENT_SOURCE))
                {
                    EventLog.CreateEventSource(EVENT_SOURCE, "Application");
                }

                EventLog.WriteEntry(EVENT_SOURCE, message);
            });
        }public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {try{using (SPSite site = properties.Feature.Parent as SPSite)
                {
                    SPWebApplication webApp = site.WebApplication;string name = "add[@name='ApplicationMasterModule']";string path = "configuration/system.web/httpModules";string value = @"<add name=""ApplicationMasterModule"" type=""Company.HttpModules.ApplicationMasterModule,Company.HttpModules, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f2391931b556f037"" />";if (webApp != null)
                    {
                        SPWebConfigModification modification = CreateModification(name, path, value);
                        webApp.WebConfigModifications.Remove(modification);
                        webApp.Update();
                        SPWebService.ContentService.ApplyWebConfigModifications();
                        SPWebService.ContentService.WebApplications[webApp.Id].Update();//Applies the web config settings in all the web application in the farmSPWebService.ContentService.WebApplications[webApp.Id].WebService.ApplyWebConfigModifications();
                    }                    
                }
            }catch (Exception ex)
            {
                WriteMessageToEventLog(ex.ToString());
            } 
        }public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            
        }public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            
        }
    }
}

 As part of the WSP, we included the following

1. Custom Master Page
2. Http Module dll which will go into GAC
3. Feature Receiver

Hope this helps. Feel free to add query as comments if you have any queries.

 
Sign Up to vote for this article
 
About Author
 
amalhashim
Occupation-Software Engineer
Company-Aditi Technologies
Member Type-Senior
Location-Not Provided
Joined date-07 Jun 2009
Home Page-http://lamahashim.blogspot.com
Blog Page-http://lamahashim.blogspot.com
I have done my masters in Computer Applications and graduation in Computer Science. I have great passion in working with Microsoft tool and technologies. I am also a Microsoft Most Valuable Professional. Personally my objective is to design/develop applications which eases user experience and performs better in long run.
 
 
Other popularSectionarticles
Comments
By:Amal HashimDate Of Posted:3/31/2011 11:11:04 PM
Re
Hi DV, Master page will go to the Master Page Gallery. You can achieve this by creating a feature. HttpModule will be a class library project. This dll will go into the GAC. In the WSP project, add this dll to the GAC folder. You can create a post build script for automatically copying the dll to the GAC folder in the WSP project.
By:DVDate Of Posted:3/31/2011 2:30:38 PM
Where to add Dll?
Hi, I used your code. but one thing I'm not getting is in solution where to add masterpage & httpmodule dll. I'm using wsp builder.
By:DVDate Of Posted:3/30/2011 8:47:46 AM
Awesummmmm!!!!
Thanks a lot.. It is exactly what I was looking for. Again Thanks a ton.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top