<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>SharePoint Magazine &#187; Application Master</title>
	<atom:link href="http://sharepointmagazine.net/tag/application-master/feed" rel="self" type="application/rss+xml" />
	<link>http://sharepointmagazine.net</link>
	<description>SharePoint Magazine is an online Magazine dedicated to the world of SharePoint</description>
	<lastBuildDate>Mon, 05 Jul 2010 09:14:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Customizing Application Master Pages &#8211; part 5 of 6</title>
		<link>http://sharepointmagazine.net/technical/development/customizing-application-master-pages-masterpages-and-sharepoint-part-5-of-6</link>
		<comments>http://sharepointmagazine.net/technical/development/customizing-application-master-pages-masterpages-and-sharepoint-part-5-of-6#comments</comments>
		<pubDate>Fri, 31 Oct 2008 10:17:41 +0000</pubDate>
		<dc:creator>ggalipeau</dc:creator>
				<category><![CDATA[Customisation]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[Application Master]]></category>
		<category><![CDATA[custom]]></category>
		<category><![CDATA[Customization]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[layouts]]></category>
		<category><![CDATA[master]]></category>
		<category><![CDATA[Master Page]]></category>
		<category><![CDATA[page]]></category>

		<guid isPermaLink="false">http://sharepointmagazine.net/?p=1449</guid>
		<description><![CDATA[This is the fith article in a six-part series on ASP.NET Master Pages and SharePoint. In this series we have discussed master pages and we have shown how to customize a master page.]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>This is the fith article in a six-part series on ASP.NET Master Pages and SharePoint.</p>
<ol>
<li><a href="http://sharepointmagazine.net/technical/development/introduction-to-master-pages">Introduction to Master Pages.</a></li>
<li><a href="http://sharepointmagazine.net/technical/development/examining-the-out-of-the-box-master-pages-in-sharepoint">Examining the out of the box Master Pages in SharePoint.</a></li>
<li><a href="http://sharepointmagazine.net/technical/customisation/developing-a-custom-master-page-master-pages-and-sharepoint-part-3-of-6">Developing a custom Master Page for SharePoint.</a></li>
<li><a href="http://sharepointmagazine.net/technical/development/deploying-the-master-page">Deploy a Master Page for a production ready system.</a></li>
<li><strong>Customizing the Application.master Page.</strong></li>
<li>Incorporating a Master Page into a SharePoint site definition<strong></strong></li>
</ol>
<p>In this series we have discussed master pages and we have shown how to customize a master page. Before we finish up the process (i.e.: incorporating master pages into a SharePoint site definition), I would like to diverge and talk about a frustrating subject &#8211; application master pages. Customizing application master pages is a little different than other master pages within SharePoint. Thus, I wanted to break this out into its own article to describe the subtle complexities of them and why they exist.</p>
<h2>What is the application master page?</h2>
<p>The application master page is the master page for application pages (also called layout pages). Thus, to answer the question &#8220;what is the application master page?&#8221;, we will discuss what an application page is. In previous articles we discussed how SharePoint web pages are a mix of templates and content data from the database. This combination is merged behind the scene and the end user sees the page. This gives us the ability to create custom pages on the fly. However, there are some pages in SharePoint that are static and used across all web applications, site collections and sites. These pages are stored in the 12 hive and are not dynamically generated with content from the database (even though they might have webparts or user controls on them). These pages are usually used for setting or admin pages. A good example is the site setting page of your site. There is an easy way to tell if a page is an application page: it will contain the word &#8220;_layouts&#8221; in the url.</p>
<p>Most articles or posts on how to customize SharePoint web pages ignore the fact that these application pages exist. The reason is that the argument can be made that only adminstrators can access these pages. Well&#8230; that can be true in certain situations, but it is all about how the site is set up out of the box. If you don&#8217;t take the necessary steps to make sure that application pages don&#8217;t show up, then they will. A good example of this is the search page (I am talking about the site search page, not the Search Center). When you do a search in SharPoint and the search scope is set to the current site, then SharePoint will display the results on a search page that is an application page. Thus, the end user will see an application page in this instance. So, as you can see, it is important to customize the application master page unless you remove all references to all application pages that the end user of the system might see. This is very important if you are trying to customize an entire portal to change the look so it doesn&#8217;t look like a SharePoint site.</p>
<p>The out of the box application master page can be found at: %Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS.</p>
<h2>Microsoft&#8217;s recommendations for customizing application.master</h2>
<p>According to this support site: <a href="http://support.microsoft.com/kb/944105">http://support.microsoft.com/kb/944105</a> &#8211; there is two ways to modify/customize the application.master page.</p>
<ul>
<li>Modify the application.master page in the 12 hive &#8211; Now&#8230; if you have been following this series of articles (or any other best practice article about SharePoint), then you will know you are NEVER suppose to modify files in the 12 hive. Other than the obvious issues this causes, including breaking the best practices Microsoft lays out, it also means we have to have the same application.master page for all web applications of a server. This means we can&#8217;t have two web applications on a server that have different look and feels.</li>
<li>Create a custom layouts folder &#8211; In my opinion this is a maintenance nightmare. You are copying your entire layouts folder to do this &#8211; not just the things that deal with application master pages. This seems like an extreme way to do something that I do not recommend.</li>
</ul>
<p>For the reasons listed above, I don&#8217;t particularly agree with either of these recommendations.</p>
<p>Luckily others in the SharePoint community agree with my thoughts here and a common solution has been introduced throughout the SharePoint community. The solution is to implement custom HttpModules.</p>
<p>This approach is a non-supported approach, but the SharePoint framework leaves us little options. And, the &#8220;supported&#8221; approaches above seem to do more damage than good, so I can live with non-supported for now.</p>
<h2>HTTP Modules</h2>
<p>Http modules intercept page requests and allow custom code to run before the page actually renders. Thus, we can intercept the request and check if it is an application page. Then we can change the master page reference dynamically as the page renders.</p>
<p><strong><em>Note</em></strong>: I originally learned the steps below from this blog by David Wise &#8211; <a href="http://www.sharepointblogs.com/dwise/archive/2007/01/08/one-master-to-rule-them-all-two-actually.aspx">http://www.sharepointblogs.com/dwise/archive/2007/01/08/one-master-to-rule-them-all-two-actually.aspx</a> . I would like to thank David to allow me to use his steps in this article posting. David would also like to give credit to K. Scott Allen whom he originally got the dynamic replacement of master pages idea for regular asp.net applications.</p>
<p><strong>Step 1 &#8211; build the project</strong></p>
<ol>
<li>Open Visual Studio</li>
<li>Create a New Project</li>
<li>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)</li>
<li>Call it ApplicationHttpModule</li>
<li>Rename Class1 to ApplicationMasterModule</li>
<li>Change the code in ApplicaitonMasterModule to look like this:</li>
<p>using System;<br />
using System.Web;<br />
using System.Web.UI;<br />
using System.IO;</p>
<p>namespace ApplicationHttpModule<br />
{<br />
public class ApplicationMasterModule: IHttpModule<br />
{<br />
public void Init(HttpApplication context)<br />
{<br />
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);<br />
}<br />
void context_PreRequestHandlerExecute(object sender, EventArgs e)<br />
{<br />
Page page = HttpContext.Current.CurrentHandler as Page;<br />
if (page != null)<br />
{<br />
page.PreInit += new EventHandler(page_PreInit);<br />
}<br />
}</p>
<p>void page_PreInit(object sender, EventArgs e)<br />
{<br />
Page page = sender as Page;<br />
if (page != null)<br />
{<br />
if (page.MasterPageFile != null)<br />
{<br />
if (page.MasterPageFile.Contains(&#8220;application.master&#8221;))<br />
{<br />
page.MasterPageFile = &#8220;/_layouts/MasterPages/Custom.master&#8221;;<br />
}<br />
}<br />
}<br />
}</p>
<p>public void Dispose()<br />
{<br />
}<br />
}<br />
}</p>
<li>Sign the project &#8211; this is needed to add the dll to the GAC
<ul>
<li>Right click on the ApplicationHttpModule project and go to the properties</li>
<li>Go to the &#8220;Signing&#8221; link on the right side</li>
<li>Click the checkbox to &#8220;Sign the assembly&#8221;</li>
<li>In the &#8220;Choose a strong name key file&#8221; dropdown, choose &#8220;&lt;New&#8230;&gt;&#8221;</li>
<li>Enter a &#8220;Key file name&#8221;.</li>
<li>Uncheck the &#8220;Protect my key with a password&#8221; checkbox</li>
<li>Click &#8220;Ok&#8221;</li>
</ul>
</li>
<li>Build the project</li>
<li>Place the dll in the GAC
<ul>
<li>If you built in Debug mode then the dll will be at: &lt;project path&gt;/bin/debug/ApplicationHttpModule.dll</li>
<li>Drag the dll (do not copy and paste) into the GAC: C:\Windows\assembly</li>
</ul>
</li>
</ol>
<p><strong>Step 2 &#8211; Register the module</strong></p>
<ol>
<li>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\&lt;port&gt;. If you did specify a different path and don&#8217;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 &#8220;Home Directory&#8221; tab. Your path will be in the &#8220;Local path&#8221; text box.</li>
<li>Add this into the web.config file within the &#8220;httpModules&#8221; section</li>
</ol>
<p>&lt;add name=&#8221;CustomApplicationHttpModule&#8221; type=&#8221;ApplicationHttpModule.ApplicationMasterModule, ApplicationHttpModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e04811459cf7ea00&#8243; /&gt;<strong> </strong></p>
<p>Note a few things:</p>
<ul>
<li>Type=&lt;class name including namespace&gt;, &lt;name of assembly&gt;</li>
<li>PublicKeyToken &#8211; 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.</li>
</ul>
<p><strong>Step 3 &#8211; Put a custom master page in the layouts folder</strong></p>
<p>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.</p>
<ol>
<li>Copy the application.master from %Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS</li>
<li>Create a new folder called &#8220;MasterPages&#8221; under %Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS</li>
<li>Paste the application.master page in the newly created MasterPages folder</li>
<li>Rename the application.master page in the MasterPages folder to Custom.master</li>
</ol>
<p><strong>Step 4 &#8211; Make changes to the Custom.master</strong></p>
<p>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.</p>
<h2>Be Careful</h2>
<p>Just like the regular SharePoint master pages you must still be careful when you start modifying application master pages. Here are a couple of things to make sure you do:</p>
<ul>
<li>Don&#8217;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&#8217;t remove it &#8211; just wrap it in a tag and turn the visible property to false.</li>
<li>You can&#8217;t have a search control on the application master pages. I am not sure of the reason for this, but you can&#8217;t have the out of the box search control on the application master pages.</li>
<li>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. Please read my blog post on this subject for more information: <a href="http://greggalipeau.wordpress.com/2008/07/30/custom-applicationmaster-working-around-microsoft-bugs/">http://greggalipeau.wordpress.com/2008/07/30/custom-applicationmaster-working-around-microsoft-bugs/</a></li>
<li>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.</li>
</ul>
<h2>Conclusion</h2>
<p>This is not as much of a conclusion, as it is a plea to the developers at Microsoft who are creating the next version of SharePoint. Please make sure we have a clear path to change application master pages. Hopefully the highly anticipated 14 hive will help with the current situation.</p>
]]></content:encoded>
			<wfw:commentRss>http://sharepointmagazine.net/technical/development/customizing-application-master-pages-masterpages-and-sharepoint-part-5-of-6/feed</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>
