I will now demonstrate how to make an application page with codebehind.
This is a task I have sometimes found useful. There’s a lot of false information and misconceptions about codebehind. Some people outright swear to controls only, because they feel codebehind should’t be used. I am not one of those. I really like codebehind Application pages.
To get a little background information about Site pages and Application pages and how they differ, and how code execution is handled, go check out my post about it here.
1: To get started create a project. I did an empty one in Visual Studio 2008.
2: Go to Project options and sign the assembly.
3: Choose your preferred namespace and assembly name.
4: Add a batch file with this in it for easy deployment and update each time you build.
Call it install.bat
@SET TEMPLATEDIR=”c:\program files\common files\microsoft shared\web server extensions\12\Template”
@SET GACUTIL=”c:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe”
Echo Installing WarhammerTypes.dll in GAC
%GACUTIL% -if bin\debug\CodeJourney.ApplicationPages.CodeBehind.dll
Echo Copying files to TEMPLATE directory
xcopy /e /y TEMPLATE\* %TEMPLATEDIR%
REM IISRESET
cscript c:\windows\system32\iisapp.vbs /a “SharePoint – 80″ /r
Note: If you use Visual Studio 2008, remember to save the file with ANSII encoding and not UTF8. Otherwise you will get errors when executing the batch file.
You can see how here
Also if you get a file not found exception when accessing the page later on, make sure gacutil copied over the DLL. Sometimes it can’t do it because of access problems. Just try again.
You can choose to use IISRESET instead of just recycling the application pool, but just recycling is much faster. You just need to know the name of the application pool.
5: Copy this to the post-build event command line by going to “Project Properties” and choose “Build events”
cd $(ProjectDir)
install.bat
6: Create the folder structure for application pages (TEMPLATE/LAYOUTS)
7: Create the aspx page in the layouts folder and place the .cs codebehind file in the root.
8: Add a reference for SharePoint.
9: In the codebehind file add the Microsoft.SharePoint and Microsoft.SharePoint.WebControls namespaces.
10: Inherit your class from LayoutsPageBase so you can easily access site and web.
11: Add reference to System.Web to be able to access Labels etc, in case it is not already there, and also add using statements for System.Web, System.Web.UI and System.Web.UI.WebControls.
12: You must make your class public in order to get this to work. I have forgotten this more than once. The reason is that this is not really like normal codebehind on an aspx page. There is no link like normally. This is obtained using inheritance really.
It now looks like this:
using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace CodeJourney.ApplicationPages{ public class MyAppPage : LayoutsPageBase{}}
Now lets get back to the application page.
13: We need to add references to SharePoint and our own DLL now. It’s with 4-part assembly names.
14: After that its time to write the page directive which is where we link up to the codebehind class.
Use Reflector to find out what the assembly names and such is.
15: We will need to do a 5-part reference to the codebehind. Add markup for testing and the aspx file now looks like this:
<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Assembly Name="CodeJourney.ApplicationPages.CodeBehind, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d216ed9999f673b4" %> <%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" Inherits="CodeJourney.ApplicationPages.MyAppPage,CodeJourney.ApplicationPages.CodeBehind, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d216ed9999f673b4" %><asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">Site Title: <asp:Label ID="lblSiteTitle" runat="server" ></asp></asp:Content><asp:Content ID="PageTitle" runat="server" ContentPlaceHolderID="PlaceHolderPageTitle"> MyAppPage.aspx</asp:Content><asp:Content ID="PageTitleInTitleArea" runat="server" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea"> My Application Page</asp:Content>
16: Let’s go back to the codebehind and add the rest so it looks like this:
using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using System.Web; using System.Web.UI.WebControls; namespace CodeJourney.ApplicationPages{ public class MyAppPage : LayoutsPageBase{ protected Label lblSiteTitle; protected override void OnLoad(EventArgs e){ SPSite site = this.Site; //Possible since we enherited from LayoutsPageBase SPWeb web = this.Web; //Same here lblSiteTitle.Text = web.Title;}}}
You can now build your project, and the aspx file will be copied to the layouts folder and the DLL to GAC (Windows\Assembly)
You can now access the page by going to http://yoursharepointurl/_layouts/MyAppPage.aspx
Have fun with application pages ![]()
Related posts:
- Repair broken Web Part Page in SharePoint / Remove Web Part From Page
- Create Simple SharePoint Hello World WebPart And Add It To a Web Part Page
- Site Pages vs Application Pages Explained & Customized vs Uncustomized
- Add A Web Part To NewForm.aspx, EditForm.aspx And DispForm.aspx (Edit Page missing in menu)
- The security validation for this page is invalid. Click Back in your Web browser…
Hi, I created a project using the above code, but I got an error on the webpage “Unknown error”. I’ve been trying to get codebehind working for a while, and it would be brest to get it working. I’ve done it pretty much exactly as you have (names etc). Could you please give me a hand. Thanks
Hello Aaron
Well its hard to say what exactly goes wrong. Have you tried debugging it by attaching to the process and go step by step? My guess is it has something to do with namespaces, class name etc. That is usually where I have gone wrong if I get your error.
If you are able to debug some lines of code, then this is not it.
Hi you can go to the web config of the site
change CustomErrors=”On” to “Off”
and change the stacktrace to “true”
then you can see the exact error
thanks
thanks
Do you know how I can call a class file in my application page ? Say I create a custom class file and want to call it in my application page. Do I need to copy the class DLL to the bin or GAC, and need to change anything in the web.config ? My application page works fine, but as soon as I make a call to my class file, ie. Dim namespace.class = New namespace.class () , I get the “Unknown Error” message. Thanks.