Code behind in layouts and master pages

22 09 2008

Something quite simple, but that has been asked me recurringly is the possibility of adding code behind to layouts and master pages. This is not only possible, but in many cases essential.
Remember that we are coding asp.net here, so you basically need to add a reference to where the code is ūüôā

I found a simple sample from Connell at MSDN (http://msdn.microsoft.com/en-us/library/bb986729.aspx).

How you do it?
* create a class 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).
* add the class to the GAC or bin folder
* add the class to the safecontrols list on your web.config
* add a reference to the class on your master or page layout file

Note: for structuring purposes, you should name this class with the same name as the file.

Here is a sample c# class:
using System;
using System.Web.UI;
using Microsoft.SharePoint.Publishing;
namespace MSDN.SharePoint.Samples {
  public class PageLayoutTemplate : PublishingLayoutPage {
  }
}

Here is how you can access it on the aspx or master file:

<%@ 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" %>

[UPDATE] On the other hand, if you need to prototype something and don’t want to create a dll, deploy it, etc, you can just add the code to the aspx file as in this example:

<script  type="text/c#" runat="server">
    protected override void OnLoad(EventArgs e)
   {
        // avoid processing if not in display mode
        if (Microsoft.SharePoint.SPContext.Current.FormContext.FormMode == SPControlMode.Display)
        {
            // testing the query string.
            // this should be achieved through a configuration key in the web.config
            if (Request.QueryString.ToString().Contains("Mercados"))
            {
                // show some; hide some other
                this.SummaryLinkFieldControl1.Visible = false;
                this.SummaryLinkFieldControl1.Visible = true;
            }
            else
            {
                // hide some; show some other
                this.SummaryLinkFieldControl1.Visible = true;
                this.SummaryLinkFieldControl1.Visible = false;
            }
        }
 }
</script>
Advertisements

Actions

Information

6 responses

13 10 2008
Dsn

Hello.
How is look MSDN.SharePoint.Samples.ContentpageTemplate and
MSDN.SharePoint.Samples.SharePointPagesWithCodeBehind class?

13 10 2008
ruimelo

Hi Dsn,

this is just an example of how you could reference those classes. In this case, you would have to build them yourself. Check the webcast on that page to have a visual guidance of what you would have to do (http://www.microsoft.com/resources/msdn/en-us/office/media/MOSS2007CodeBehindPublishing.asx).

Cheers,

Rui

31 03 2009
Marius C.

if I’m not mistaking, using of the inline script in your page (as mentioned in your UPDATE section) it will no longer work as soon as someone Unghosts your page via SP Designer. That was one of the reasons to deploy it via assembly as SharePoint will no longer render your page in case of inline script (check Andrew Connell’s video on MSDN about code-behind in Master pages and page layouts)

Regards,
Marius C.

2 04 2009
ruimelo

Hi Marius,

Good point and that is probably correct.
As a thumb rule, I don’t allow SPdesigner in production environments ;-). Anyway, adding the code inline, as I mention, should only be used for a fast PoC (Proof of Concept) or prototyping. Never for a final solution.
Thanks for your input!

25 11 2009
seilbowmetime

A lot of of people blog about this topic but you wrote down some true words.

7 10 2011
Alex C

Very nice technique. Just started using this on my page layout. Didn’t even know you could do this. Thank you so much for sharing this along with code samples ūüôā

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: