Back

TechnologyJan 04, 2010

Custom ASPX pages (w/ code behind) in SharePoint

Credera Team

There are several different ways to add custom ASPX pages with code behind to SharePoint. The following link gives an overview of some of the most common ways to do this (http://sharenotes.wordpress.com/2008/02/21/add-custom-aspx-pages-or-asp-net-pages-in-sharepoint/) ; however, the below approach in this article is a new alternative which I find to be fairly quick, easy, and allows us to still utilize SharePoint’s security without needing to build and deploy a feature.  This is a good approach if your pages are going to live in soley one place without the need to be reusable like webparts.

To make your custom ASPX page ready for Sharepoint, you will need to perform the following in the code behind ([filename.aspx].cs):

  • add all event handlers dynamically in the page load (we will also remove any event handler references in the aspx mark up)

  • Reference the Microsoft.SharePoint and Microsoft.SharePoint.Publishing assemblies.  These are usually found at C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12ISAPI and C:Program FilesMicrosoft Office Servers12.0Bin

  • Implement Microsoft.SharePoint.Publishing.PublishingPageLayout instead of System.Web.UI.Page

[code lang=”c#”]

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Publishing;

namespace SharePointCustomPageTest

{

public partial class _Default : Microsoft.SharePoint.Publishing.PublishingLayoutPage

{

protected void Page_Load(object sender, EventArgs e)

{

this.btnClear.Click += this.btnClear_Click;

this.btnSubmit.Click += this.btnSubmit_Click;

}

protected void btnSubmit_Click(object sender, EventArgs e)

{

int i = 0;

int i2 = 0;

int i3;

try

{

if (txt1.Text.Length > 0)

i = Convert.ToInt16(txt1.Text);

if (txt2.Text.Length > 0)

i2 = Convert.ToInt16(txt2.Text);

txt3.Text = (i + i2).ToString();

}

catch (Exception ex)

{

txt3.Text = ex.Message + ex.StackTrace;

}

}

protected void btnClear_Click(object sender, EventArgs e)

{

txt1.Text = string.Empty;

txt2.Text = string.Empty;

txt3.Text = string.Empty;

}

}

}

[/code]

In your aspx mark-up, make the following adjustment:

  • Remove all references to event handlers (e.g. onclick=”… )

[code lang=”c#”]

%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”Default.aspx.cs” Inherits=”SharePointCustomPageTest._Default” %>

http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

http://www.w3.org/1999/xhtml” >

Number 1:

Number 2:

[/code]

Build your project assembly (signing the assembly), adding it to your server’s gac (or web application bin), as well as registering the assembly as a SafeControl in your SharePoint web.config.

Increase business process efficiency and collaboration with Microsoft SharePoint

Explore Our Microsoft Consulting Services  →

The final task is to get our aspx pages ready for SharePoint.  Our aspx pages should basically follow the structure of page layouts (you can open and copy one of these using SharePoint Designer).  Build your new aspx pages using the example structure shown below, placing our aspx code in the PlaceholderMain content placeholder, as well as adding reference to our assembly in the Inherits statement.  I recommend this “Sharepoint-ready” aspx page be built external to your original project, as once the below changes are implemented to your page, your project will not build any longer.

[code lang=”c#”]

<%@ Page Language=”C#” CodeBehind=”Default.aspx.cs” Inherits=”SharePointCustomPageTest._Default, SharePointCustomPageTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=acec0186600f0729″ %>

Number 1:

Number 2:

[/code]

Once your page is ready, simply upload it into any SharePoint library (e.g. Pages library).