SharePoint 2010 Creating a Site Page with code behind

With Visual Studio 2010 it’s easy to create an application page for SharePoint 2010. But if you create an application page, you will realize immediatly that this page will be published in the _layouts folder of SharePoint and is therefore available in the whole Web Application. This is nice because you can add pages with code behind, which are available for all users. But what if you just want to have a site page with code behind? Let me show you how to achieve this.

But before, let me tell you that you should be aware of some things. If you create a site page with code behind and your users edit this page with the SharePoint Designer it might fail or occur an error. If you need more information on this, please read this article.
Therefore please see this post not as recommendation or advice to use code behind for site pages, but to show you how you can achieve this, if you need it.

Step 1: Add an application page

In your Visual Studio Project add an application page. Visual Studio should now create automatically a mapped folder to the folder Controltemplates of the 14 Hive of SharePoint and store the application page there.

Step 2: Create a module for your site page

In your Visual Studio Project add a module. This module has a sample.txt file. You can delete it. Drag you application page from controltemplate folder and drop it it in your module.

 Step 3: Customize the application page

You aspx page should look similiar to this one. You should pay attention to the red marked lines. There are changes necessessary:

<%@ Assembly Name=”$SharePoint.Project.AssemblyFullName$” %>
<%@ Import Namespace=”Microsoft.SharePoint.ApplicationPages” %>
<%@ 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″ %>
<%@ Import Namespace=”Microsoft.SharePoint” %>
<%@ Assembly Name=”Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Page Language=”C#”
AutoEventWireUp=”true”
CodeBehind=”ChooseChecklist.aspx.cs”
Inherits=”YourProjectName.YourModuleName.YourClassName”
MasterPageFile=”../_catalogs/masterpage/V4.master” %>

<asp:Content ID=”PageHead” ContentPlaceHolderID=”PlaceHolderAdditionalPageHead” runat=”server”> </asp:Content>
<asp:Content ID=”Main” ContentPlaceHolderID=”PlaceHolderMain” runat=”server”> </asp:Content>
<asp:Content ID=”PageTitle” ContentPlaceHolderID=”PlaceHolderPageTitle” runat=”server”> </asp:Content>
<asp:Content ID=”PageTitleInTitleArea” ContentPlaceHolderID=”PlaceHolderPageTitleInTitleArea” runat=”server” > </asp:Content>

Step 4: Customize the Elements.xml

In the module you’ll find a file elements.xml.
<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<Module Name=”YourModuleName”>
<File Path=”YourModuleName\Your.aspx” Url=”YourModuleName/Your.aspx” />
</Module>
</Elements>

If you deploy your solution, you’ll find your page at the site http://yoursite.com/YourModuleName/Your.aspx

Hope this helps you. You can also read this article for more information.

..:: I LIKE SHAREPOINT ::..

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

About Karsten Schneider 312 Articles
Consultant for Microsoft 365 Applications with a strong focus in Teams, SharePoint Online, OneDrive for Business as well as PowerPlatform with PowerApps, Flow and PowerBI. I provide Workshops for Governance & Security in Office 365 and Development of Solutions in the area of Collaboration and Teamwork based on Microsoft 365 and Azure Cloud Solutions. In his free time he tries to collect tipps and worthy experience in this blog.

1 Comment

Leave a Reply