Applies to: Microsoft Windows SharePoint Services 3.0, Microsoft Office SharePoint Server 2007, Microsoft Visual Studio 2005
Ted Pattison, Ted Pattison Group
May 2007
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.
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.
<%@ 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>
<%@ 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.
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.
@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.
SPSite siteCollection = SPContext.Current.Site; SPWeb site = SPContext.Current.Web;
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.
Watch the Video
Video Length: 00:04:57
File Size: 13.2 MB WMV
Sample Code: Custom Application Pages
Item-Level Auditing with SharePoint Server 2007
Windows SharePoint Services Developer Center
SharePoint Server 2007 Developer Portal
Office Developer Center
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.
The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)Also all the videos have problem.thanksPankajChange the assembly name value to
"Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
http://wsslayouts.blogspot.com
[tfl - 30 04 09] You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about: Visual Studio : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C& .NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&
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...
I'm working on a public-facing internet site with some application pages, the web application and site collection are enabled Anonymous Access, but when Anonymous user views any application pages, the window login popup.
My question is, How to give Anonymous user view permission without any login process?
Thanks,
Gary