Click to Rate and Give Feedback
MSDN
MSDN Library
Office Development
Visual How Tos
 Adding Code-Behind Files to Master ...
Community Content
In this section
Statistics Annotations (9)
Adding Code-Behind Files to Master Pages and Page Layouts in SharePoint Server 2007

Summary: Learn to use code-behind files within master pages and page layouts that are used in Microsoft Office SharePoint Server (MOSS) 2007 Web content management (WCM) publishing sites.

Office Visual How To

Applies to: Microsoft Office SharePoint Server 2007, Microsoft Visual Studio 2005

Andrew Connell, Andrew Connell Inc. (Microsoft MVP)

January 2008

Overview

Many things ASP.NET 2.0 developers are used to building can seem challenging within a Microsoft Office SharePoint Server (MOSS) 2007 environment. However, because the MOSS 2007 architecture is based on ASP.NET 2.0, developers can now use nearly all the same techniques they implemented in ASP.NET 2.0 sites in SharePoint sites they develop. One of these techniques is to make use of code-behind files within master pages and page layouts that are used in MOSS 2007 Web content management (WCM) publishing sites.

Code It

First, create a class file (Microsoft Visual C# in this example) in a Class Library project that inherits from the Microsoft.SharePoint.Publishing.PublishingLayoutPage class (or System.Web.UI.MasterPage if you are creating a code-behind file for a master page). You should name this class with the same name as the file, but it is not required.

C#
using System;
using System.Web.UI;
using Microsoft.SharePoint.Publishing;

namespace MSDN.SharePoint.Samples {
  public class PageLayoutTemplate : PublishingLayoutPage {
  }
}

Wiring the Code-Behind to the Master Page or Page Layout

Next, wire the code-behind file to the master page or page layout. This is done by entering the fully qualified name, otherwise known as the five-part name, in the Inherits attribute of the page layout or master page directive. The five-part name is the full type name (namespace.class) followed by the strong name of the signed assembly.

C#
<%@ Page meta:progid="SharePoint.WebPartPages.Document" Language="C#" _ 
   Inherits="MSDN.SharePoint.Samples.ContentpageTemplate, _
   MSDN.SharePoint.Samples.SharePointPagesWithCodeBehind, Version=1.0.0.0, _
   Culture=neutral, PublicKeyToken=ae015afe5f30fb68" %>
<%@ Master language="C#" Inherits="MSDN.SharePoint.Samples.MinimalMasterTemplate, _
   MSDN.SharePoint.Samples.SharePointPagesWithCodeBehind, Version=1.0.0.0, _
   Culture=neutral, PublicKeyToken=ae015afe5f30fb68" %>

At this point the code-behind file is wired up to the page layout or master page and developers can do everything they are used to doing within ASP.NET 2.0 development, such as overriding the OnLoad() method or bind data to controls on the page.

Read It

Adding code-behind files in ASPX or ASCX files within a traditional ASP.NET 2.0 environment is quite simple thanks to Microsoft Visual Studio. All a developer has to do is right-click the file in Visual Studio either in Solution Explorer or in the designer, and then click View Code. Visual Studio then creates a new code-behind file (if one did not already exist), adds in the minimal code for the new class to inherit from the Microsoft.SharePoint.Publishing.PublishingLayoutPage class, and then adds the necessary information to the Inherits attribute in the Page or Control directive in the ASPX or ASCX file. This tells the Microsoft .NET Framework where to find the associated class for the file. Developers must inherit from the Microsoft.SharePoint.Publishing.PublishingLayoutPage class and not the System.Web.UI.Page class, as the former dynamically sets the master page URL using the current site's CustomMasterUrl property. Unfortunately, Visual Studio can't do the same things for custom page layouts used in MOSS 2007 publishing sites. Instead the developer must do these things manually. The same is true for master pages in SharePoint sites.

One word of caution: Do not add inline code to the page. Put all code in a code-behind file. Why? Because when a page is uncustomized (or when the source of the page is still pulled from the file on the file system rather than from the database), MOSS allows the page to run normally. However, if the page is customized by using a tool such as Microsoft Office SharePoint Designer 2007, the page is run through the MOSS safe mode parser, which will block any inline code from running.

See It Adding Code-Behind Files in SharePoint Server 2007

Watch the Video

Video Length: 00:14:12

File Size: 13.7 MB WMV

Explore It
Community Content   What is Community Content?
Add new content RSS  Annotations
hi andrew, great demo!      jendxb ... Red-Devile   |   Edit   |  
...can you please also make the project for this available for download? xx

This does not make a developers job easy      Larry13   |   Edit   |  
Why has Microsoft made adding a code-behind page so convoluted and freaking stupid. I think you should tell developers to stand on their heads while they type in this code. That would be a little more logical.
Tags What's this?: Add a tag
Flag as ContentBug
I must have missed something      Chris Lively   |   Edit   |  

Did we just enter some sort of time warp? One that takes us back to the ancient days before drag and drop, data binding, and point and click development? Surely this is indicative of programming in 1998 not 2008. I take that back. Coding in 1998 wasn't this complicated either.


Tags What's this?: Add a tag
Flag as ContentBug
Responding to comments      Andrew Connell [MVP]   |   Edit   |  

jendxb-
I don't have the code available for download... but it should be pretty easy to throw it together folowing the VHT.

LRP13 & ChrisL-
It's no secret that the SharePoint development experience is lacking and MSFT is working to address that. So for how you can just decide to complain, or work to figure out how to address it to make it work the way you need it to work, as I demonstrated here in this VHT. Is it REALLY that hard to manually wire up a code behind with an ASPX? No... not at all. In fact, I'd argue it's a good exercise so people understand what the development tools do for them. Otherwise when stuff breaks, you don't know how to fix it.

The only question      Alec Pojidaev ... NGDev   |   Edit   |  

The only question I have is how absence of reference to PageLayoutTemplate class in ASP code helps to understand the concept?

Tags What's this?: Add a tag
Flag as ContentBug
Great Example      Phantomcodeing   |   Edit   |  
This is very similar to master pages in non sharepoint sites shows how programing in 2008 is so much more efficent than in 1998!
Wounderfull example
Tags What's this?: Add a tag
Flag as ContentBug
PublishingAssociatedContentType Problem      jendxb   |   Edit   |  

Hi Andrew, thanks for the "encouragement" ...yes, it was in fact not too difficult to put the source code together following the concept of your VHT ;-)

there's one little glitch i encountered though ...when specifying the PublishingAssociatedContentType as ";#Article Page;#0x010100C...", as shown in the VHT, the pagelayout ends up not being associated properly with the "Page Layout Content Types" Content Type Group.

Instead, the following statement worked for me...

<Property Name ="PublishingAssociatedContentType" Value=";#$Resources:cmscore,contenttype_articlepage_name;;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D;#"/>

Thanks again for the great VHT!

jennifer www.finalcandidate.com

Tags What's this?: Add a tag
Flag as ContentBug
PublishingPageLayout Requires FullTrust      michhes   |   Edit   |  

Still sorting through the details but PublishingPageLayout makes a FullTrust demand so running a custom cas policy probably won't work with this approach. WebPartPage doesn't make the same demand but I can't get it to work without FullTust either.

Michhes (http://blog.mediawhole.com)

Flag as ContentBug
How do we achieve the same with WSS 3.0?      Kulkarni Manoj Mohan   |   Edit   |  
In WSS 3.0 there is no publishing feature, how do i assign different master page for a page layout?
(i.e. how to i assign different master page to spstd1.aspx to spstd9.aspx)
Tags What's this?: Add a tag
Flag as ContentBug