Share via


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.

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.

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