Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Server Technologies
Visual How Tos
 Creating an Application Page in Win...
Community Content
In this section
Statistics Annotations (10)
Creating an Application Page in Windows SharePoint Services 3.0
Visual How To

Applies to: Microsoft Windows SharePoint Services 3.0, Microsoft Office SharePoint Server 2007, Microsoft Visual Studio 2005

Ted Pattison, Ted Pattison Group

May 2007

Overview

You can create custom application pages to add user interface components to a custom solution based on Microsoft Windows SharePoint Services 3.0. Unlike site pages (for example, default.aspx), a custom application page is deployed once per Web server and cannot be customized on a site-by-site basis. Application pages are based in the virtual _layouts directory. In addition, they are compiled into a single assembly DLL. They are also used across all sites within a server farm. For these reasons, they perform better than site pages. With application pages, you can also add inline code. With site pages, you cannot add inline code.

Code It

Typically, you link custom application pages to application.master, the master page file that is used by the default application pages in Windows SharePoint Services. You should also write application pages to inherit from a base class defined inside the Microsoft.SharePoint assembly named LayoutsPageBase. The following example provides the basic layout for a custom application page.

C#
<%@ Assembly Name="Microsoft.SharePoint, [full 4-part assembly name]"%> 
<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" 
         Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase" %>

<%@ Import Namespace="Microsoft.SharePoint" %>

<script runat="server">
  protected override void OnLoad(EventArgs e) {
    SPWeb site = this.Web;
    lblSiteTitle.Text = site.Title;
    lblSiteID.Text = site.ID.ToString().ToUpper();
  }
</script>

<asp:Content ID="Main" runat="server"
             contentplaceholderid="PlaceHolderMain" >
  Site Title: <asp:Label ID="lblSiteTitle" runat="server" />
  <br/>
  Site ID: <asp:Label ID="lblSiteID" runat="server" />
</asp:Content>

<asp:Content ID="PageTitle" runat="server" 
             contentplaceholderid="PlaceHolderPageTitle" >
   Hello World
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" runat="server"
             contentplaceholderid="PlaceHolderPageTitleInTitleArea" >
   The Quintessential 'Hello World' of Application Page
</asp:Content>
Visual Basic
<%@ Assembly Name="Microsoft.SharePoint, [full 4-part assembly name]"%> 
<%@ Page Language="VB" MasterPageFile="~/_layouts/application.master" 
         Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase" %>

<%@ Import Namespace="Microsoft.SharePoint" %>

<script runat="server">
  Protected Overrides Sub OnLoad(ByVal e As EventArgs)
    Dim site As SPWeb = Me.Web
    lblSiteTitle.Text = site.Title
    lblSiteID.Text = site.ID.ToString().ToUpper()
  End Sub
</script>

<asp:Content ID="Main" runat="server"
             contentplaceholderid="PlaceHolderMain" >
  Site Title: <asp:Label ID="lblSiteTitle" runat="server" />
  <br/>
  Site ID: <asp:Label ID="lblSiteID" runat="server" />
</asp:Content>

<asp:Content ID="PageTitle" runat="server" 
             contentplaceholderid="PlaceHolderPageTitle" >
   Hello World
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" runat="server"
             contentplaceholderid="PlaceHolderPageTitleInTitleArea" >
   The Quintessential 'Hello World' of Application Page
</asp:Content>

Deployment

Note that although you can deploy your application pages directly inside the \LAYOUTS directory, doing so can create file name conflicts between your application pages and those application pages that are created by Microsoft and other companies. It is a best practice to deploy your application pages inside a company-specific or project-specific directory that is nested with the \LAYOUTS directory. For example, you can deploy application pages within a company-specific directory located at the path \LAYOUTS\Litware.

Read It

You usually create custom application pages with a link to the application.master file. The custom application page demonstrated in this How-To sample adds three Content tags to add HTML content to the resulting page. In particular, this page replaces three placeholders that are defined inside application.master: PlaceHolderMain, PlaceHolderPageTitle and PlaceHolderPageTitleInTitleArea. However, these are only three of the many different placeholders that you can choose to replace.

Also note that an application page can have a script block at the top with code that programs against the object model. When you program a similar page within Microsoft Visual Studio 2005, you can benefit from conveniences such as color-coding and Microsoft IntelliSense. However, you must add the correct @Assembly directive to the top of the page to reference the Microsoft.SharePoint assembly.

Application pages are useful because they provide quick and easy access to the Windows SharePoint Services object model. After you create an application page and provide an overridden implementation of the OnLoad method, you can obtain entry points into the Windows SharePoint Services object model in a site-specific context by using the following code.

C#
SPSite siteCollection = SPContext.Current.Site;
SPWeb site = SPContext.Current.Web;
Visual Basic
Dim siteCollection As SPSite = SPContext.Current.Site
Dim site As SPWeb = SPContext.Current.Web

The ability to obtain site-relative context makes writing application pages far more powerful. An application page can function differently depending on which site you go through to access it. When you navigate to an application through the context of one site, it typically appears and functions differently than when you navigate to it through the context of another site.

See It Create Custom Application Page

Watch the Video

Video Length: 00:04:57

File Size: 13.2 MB WMV

Explore It
Community Content   What is Community Content?
Add new content RSS  Annotations
Ted, *.cs files are missing!      MauSOliveira ... frank-ove   |   Edit   |  
It's a very nice article, except by a mistake: I've downloaded the zipped file with sample code, but all *.cs files are missing!

Nevermind this comment. All the code is inline, i.e. inside the .aspx files.
Tags What's this?: Add a tag
Flag as ContentBug
Video      Bert Groenveld   |   Edit   |  
Please make the video downloadable. The supplied stream often freezes.
Tags What's this?: Add a tag
Flag as ContentBug
Video      Cled   |   Edit   |  
Yes, a downloadable video would be superb; I have had the same issues.
Tags What's this?: Add a tag
Flag as ContentBug
.      steevjobz   |   Edit   |  
this video is not working, after 1 minute of play it freezes, only the sound keeps going, i'v tried windows media player 11 and the 9 neither work.
Tags What's this?: video (x) Add a tag
Flag as ContentBug
Video is bad      Fred Morrison   |   Edit   |  
The video doesn't work. After about a minute, the audio continues but the video stops updating. Please redo this video and repost.
Tags What's this?: video (x) Add a tag
Flag as ContentBug
VS show errors      Benayto ... Noelle Mallory - MSFT   |   Edit   |  

Isn't there a way to avoid Visual Studio from showing errors when building this code? It doesn't find the master page nor the contentPlaceHoldersId's. And the Assembly reference generates an "HRESULT exception: 0x80131417". But the page works fine if you to open it at your explorer.

[Noelle Mallory - MSFT] Please post questions to the MSDN Forums at http://forums.microsoft.com/msdn. You will likely get a quicker response through the forum than through the Community Content.

Tags What's this?: Add a tag
Flag as ContentBug
Giving Error      Pankaj1089 ... Rick Kierner   |   Edit   |  

The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
Also all the videos have problem.

thanks
Pankaj

Change the assembly name value to

"Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"

Tags What's this?: Add a tag
Flag as ContentBug
hey Explain how to add additional page layouts to wss 3.0      Adding Custom Layout Pages to WSS 3.0   |   Edit   |  
I noted some points here

http://wsslayouts.blogspot.com

But , i need some more explanation on few points such as "OWSSVR.DLL"" that comes into play.
Tags What's this?: Add a tag
Flag as ContentBug
File not found?      mtownsend ... FaulstiR   |   Edit   |  
Hi all,

While hello.aspx works just fine, when I try to view any of the ApplicationPageX.aspx files, I get "File Not Found". Since it's the only difference between ApplicationPage1 and Hello, I'm guessing that the error is to do with this line:
Inherits="CustomApplicationPages.ApplicationPageX"

It seems to work fine in the video demo and I haven't changed any of the code... Any ideas on what's wrong?


Nevermind, I fixed it. The CustomApplicationPages.dll wasn't being installed in the GAC because the lines doing so in install.bat were commented out for some reason...

Tags What's this?: Add a tag
Flag as ContentBug
How to give Anonymous user to view Application Page      Gary G. Wang   |