In most custom SharePoint solution projects you will have to create your own Application Pages, Web Part Pages or other ASP.NET pages to fulfill your requirements. If you are coming from the ASP.NET world you are pretty used to building web forms and similar .aspx pages. Your pages will most of the times then be derived from the System.Web.UI.Page class, but when working with SharePoint you should not use this class as a base.

Instead you should always use one of the following base classes provided in the Microsoft SharePoint APIs.

LayoutsPageBase class

The LayoutsPageBase (in Microsoft.SharePoint.WebControls) class is the most common class to derive application pages from. This is a very basic class but makes your life a whole lot easier in the SharePoint world.

Pages derived from this class should always reside in the _layouts folder. These pages are called Application Pages (or _layouts pages) and are allowed to contain custom code, compared to Content Pages which reside in SharePoint (such as Web Part Pages). These pages cannot contain customizable Web Parts or Web Part Zones. Creating Content Pages is another story.

The advantages with using the LayoutsPageBase as your base class is that you can easily access the current SharePoint Site or Site Collection with the built-in properties and control the security of the application page.

Access the SharePoint objects

With the LayoutsPageBase class you can use the built-in properties for the Site and Web to access the current Site Collection or Site (both these properties are derived from the UnsecuredLayoutsPageBase class, see below) or use the SPContext class to access the current site and web.

Stop long running operations

You have most certainly created some pages that creates reports or similar that may take a long time to generate and consumes server resources. Some times it takes so long that the user shut downs the web browser and go drink a cup of coffee instead. If you have those kind of pages you should use the StopRequestIfClientIsNotValid method. This method ends the request if the client is no longer connected to the page and saves you of some CPU cycles. On the other hand, if you have these kind of pages - think over and use the SPLongOperation class to inform the user that it will take a while.

The page will also automatically call this method in several of the page events, such as OnLoad and Render.

Security in the Application Page

The LayoutsPageBase class contains a virtual property called RightsRequired, this property can be used to programatically set which rights (on the current Site) that are required to use the application page. By default the rights are checked at the end of the OnLoadComplete, but using the RightsCheckModes property you can disable the check or perform it in OnPreInit instead.

There are also a property called RequireSiteAdministrator that can be overridden to make sure that the user is site administrator.

Exit from the Application Page

If you are creating an application page the uses the ButtonSection control template you will have a Cancel button. The target of this Cancel button is controlled using the PageToRedirectOnCancel property. Just override the property and return a string containing the target of your cancel page.

UnsecuredLayoutsPageBase class

The LayoutsPageBase class derives from the UnsecuredLayoutsPageBase class, which derives from the System.Web.UI.Page. Only derive from this one directly if you need anonymous and non-authenticated users to access your application page. Most of the methods and properties used in the LayoutsPageBase class comes from this class.

For example the Login Page uses this one as a base.

This was a quick post to get you started with creating custom application pages in SharePoint. So what are you waiting for…