Creating a SharePoint Site Page With Code-Behind Using Visual Studio 2010

Published on: Author: Rob Windsor Leave a comment

A while ago I was reading Kirk Evans blog post on the subject of using code-behind in SharePoint 2010 site pages. I wanted to make sure things still worked the way they did in SharePoint 2007. Everything looked the same until I saw the part about updating the web.config file.


From Kirk’s blog post:


“The problem is that SharePoint has a setting that disallows server-side code with [site] pages.  This is a security feature that is good (you really don’t want end users to arbitrarily inject server-side code), but there may be cases where you are OK with some of your users having this capability.  For instance, you can have a page that is only visible to a small team within your enterprise, and one of the team members is very technical and wants to provide some custom code for SharePoint.  Party on, have fun with it, it saves my team from having to write that code.

To enable this scenario (and enable the Button_Click event handler in our code), we need to add an entry to web.config.  Knowing that we can’t just go to every front-end web server and make the modifications (any admin worth his salt should slap you silly for even thinking about hand-modifying multiple web.config files in a production farm), we should provide this as part of our solution.”


OK, I remember that in SharePoint 2007 you could add an entry in web.config to indicate the page parser should allow inline code for a page, but you didn’t have to do so. As long as the markup didn’t have anything indicating that code should run you were good. So I took Kirk’s example and modified it a bit so it would work without changes to the web.config.


In the markup, I removed the namespace imports, the CodeBehind attribute of the Page element, and the OnClick attribute of the Button element.


<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Register 
    Tagprefix="SharePoint" 
    Namespace="Microsoft.SharePoint.WebControls" 
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register 
    Tagprefix="Utilities" 
    Namespace="Microsoft.SharePoint.Utilities" 
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register 
    Tagprefix="asp" 
    Namespace="System.Web.UI" 
    Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Page Language="C#" 
    Inherits="SampleToDeployAPage.MyPageTemplate" 
    Title="Testing This Page"
    MasterPageFile="~masterurl/default.master" 
    meta:progid="SharePoint.WebPartPage.Document" %>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
  <asp:Button runat="server" ID="button1" Text="Click me" />
  <asp:Label runat="server" ID="label1"/>
</asp:Content>

Of course, I needed to make changes to the code behind file to compensate for what I removed from the markup – but the changes were minor. All I needed to do was hookup the Button_Click method to the Click event of the Button. In WebForms, attaching event handlers is done in the Page_Init event.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;

namespace SampleToDeployAPage
{
    public class MyPageTemplate : WebPartPage
    {
        protected Button button1;
        protected Label label1;

        protected void Page_Init(object sender, EventArgs e)
        {
            button1.Click += new EventHandler(Button_Click);
        }

        protected void Button_Click(object sender, EventArgs e)
        {
            label1.Text = System.DateTime.Now.ToLongTimeString();
        }
    }
}

I kept everything else the same (minus the web.config changes of course), deployed, navigated to the page, clicked the button and SHAZAM! -  the date showed.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>