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 (12)
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, Critical Path Training, LLC (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   |   Show History
...can you please also make the project for this available for download? xx

This does not make a developers job easy      Larry13   |   Edit   |   Show History
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.
I must have missed something      Chris Lively   |   Edit   |   Show History

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.


Responding to comments      Andrew Connell [MVP]   |   Edit   |   Show History

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   |   Show History

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   |   Show History
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   |   Show History

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   |   Show History

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?      manoj2784   |   Edit   |   Show History
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
Some Question      RaymondLam   |   Edit   |   Show History

i follow the step by step , but why SharePoint Designer auto add the following code in my Page Template

it cause Exceptioin

<html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"><head>
<META name="WebPartPageExpansion" content="full">
<!--[if gte mso 9]><xml>
<mso:CustomDocumentProperties>
<mso:ContentTypeId msdt:dt="string">0x01010007FF3E057FA8AB4AA42FCB67B453FFC100E214EEE741181F4E9F7ACC43278EE81100530754E35E21CC44A05F9AF48B535E2F</mso:ContentTypeId>
</mso:CustomDocumentProperties>
</xml><![endif]-->
</head>
Tags What's this?: Add a tag
Flag as ContentBug
A way to get this to work more like asp.net development      Rangler   |   Edit   |   Show History
If you use VS2008 or VS2005SP1 project "Web Application Project" which is like Website compiled into class library, then you can add ASPX pages and use the coupled code behind view, including drag and drop, double click on buttons, etc. You just have to change the INHERITS of the Page so it uses the 5 part name, and change the INHERITS of the code behind to use PublishingPageLayout.

In order to get Design view working for a page layout you have to add a MasterPageFile declaration temporarily so it will let you edit content within asp:Content tags. You'll need to remove the MasterPageFile declaration before putting it into SharePoint, as even though SharePoint sets the MasterPageFile it seems to use your own if you put it in. You need to keep in the CodeBehind attribute so VS knows which .cs file to put the code into.

I'm using this for Page Layouts but i don't see why it wouldn't work for master pages as well, but you're less likely to need the coupled functionality for them.

I'm using WSPBuilder to create the WSP solution file from this project, which means all I have to do is have a feature.xml and it's related xml file in order to work on page layouts.

Hope this helps someone. Maybe i'll get a blog one day and set up a demo.

Andy

Tags What's this?: Add a tag
Flag as ContentBug
Urgent Query..      urs.yash   |   Edit   |   Show History
Hi Andrew Connell..
First of all thanks a lot for giving us information that we can use a code behind file with master page in moss.. i am in great need of that..
I am specifically having query related to code behind file for Master Page.. u have given example but havent made clear that where this master page with code behind file have to be linked with moss.. like where it had to deploy, where its dll file should be placed so that it can be used by a share point site.. please made it clear.. also the video demo is not working and the contet is not available for download.. PLEASE REPLY..
Thanks..
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker