Creating Application Pages for SharePoint Sites

SharePoint Foundation 2010 provides some flexibility when developing pages for your SharePoint environment. One of the nice features about SharePoint is having the ability to create application pages. You can identify an application page by the text _layouts in its URL, such as the site settings page. Application pages are deployed once per SharePoint server and have the ability to be shared across all sites and Site Collections in the farm. They also have the ability to contain code that runs behind the page that is compiled into a single dynamic link library (DLL), which improves performance.

Most of the typical pages are content pages. Content pages can be customized with an application such as SharePoint Designer, whereas application pages cannot be customized. Also, application pages can include inline code and page markup, whereas content pages cannot because this could be accidentally manipulated by users with SharePoint Designer so that it does not work correctly, or manipulated with malicious intent.

Therefore, if you want to create a page that will contain some heavy custom code, and you want that capability across all sites, application pages are the way to go.

Note

SharePoint Foundation incorporates a Modal Dialog framework that allows users to stay in context of the page to see additional information that is related to the page without navigating away from the page, for example, displaying the properties of a list item. To read more about how to use the Modal Dialog framework with application pages, go to http://blog.mastykarz.nl/sharepoint-2010-application-pages-modal-dialogs.

Visual Studio 2010 provides out-of-the-box templates with which developers can quickly create an application page in Visual Studio. The following steps outline how to create a SharePoint application page:

Note

For more information about using Visual Studio to create SharePoint Foundation solutions and how to set up your development environment, see Chapter 16.

  1. Open Visual Studio with administrative privileges. On the Start Page tab, click New, Project, or on the toolbar, click File, select New, and then click Project to open the New Project dialog box.

  2. Under Installed Templates, under the appropriate language (such as Visual C#), expand SharePoint, if it is not already expanded, and then click 2010.

  3. In the middle pane, select Empty SharePoint Project.

    This is the most basic SharePoint project type; it must be used for application pages because they are only available as project items.

    image with no caption
  4. In the Name textbox, type SharePointApplicationPage1 for the Project Name, and then click OK.

    The SharePoint Customization Wizard opens.

  5. Specify the site on which you want to deploy and test the application page, and then select Deploy As A Farm Solution for the trust level.

    Application pages are deployed on the server and are available to all sites; therefore, they are deployed at the farm level. Sandboxed solutions are scoped at a Site Collection level and can be deployed without administration privileges. Click Finish to close the SharePoint Configuration Wizard.

  6. In Solution Explorer, right-click the SharePointApplicationPage1 project, select Add, and then click New item.

  7. In the Add New Item dialog box, in the middle pane, select Application Page. In the Name textbox, type CustomApplicationPage1, and then click Add.

Visual Studio automatically creates the required files to create a basic application page and opens the source view for the ASPX file. Visual Studio does not provide any WYSIWYG editing environment for applications pages. If you add other application pages to this project they will be stored in the SharePointApplicationPage1 folder, in the Layouts folder.

image with no caption

Note

The Project now contains a Layouts folder. This folder maps to the _Layouts virtual directory created by the SharePoint server, which on the file system is the SharePoint root TEMPLATELAYOUTS subfolder. You will also notice that it has created a subfolder called SharePointApplicationPage1. This will contain the application pages for this project. When the pages are deployed, they will automatically be placed under the _layouts directory in the same folder directory that they are listed under in Solution Explorer.

The initial markup for the application page is shown in the following code block:

<%@ 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="CustomApplicationPage1.aspx.cs"
  Inherits="SharePointApplicationPage1.Layouts.SharePointApplicationPage1.
  CustomApplicationPage1" DynamicMasterPageFile="~masterurl/default.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">
Application Page
</asp:Content>

<asp:Content ID="PageTitleInTitleArea"
   ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea"
   runat="server" >
My Application Page
</asp:Content>

When the assembly is built, it will be deployed to the Global Assembly Cache (GAC). The assembly parameter $SharePoint.Project.AssemblyFullName$, will be replaced with the full assembly reference—the full assembly name, version number, culture and Pubic Key Token.

The @Page directive points to the code-behind file and links the application page with a master page by using the DynamicMasterPageFile attribute. This attribute is set to the token ~masterurl/default.master, which when the user requests the application page is replaced on the server with the site’s default master page. Unless you have customized the default master page, the token will be replaced with v4.master.

The sample application page contains four content placeholders:

  • PlaceHolderAdditionalPageHead Content added to this placeholder appears in the page’s <HEAD> tag.

  • PlaceHolderMain This is the main content area of the page.

  • PlaceHolderPageTitle Content added in this placeholder appears in the browser’s title bar.

  • PlaceHolderPageTitleInTitleArea Content added in this placeholder appears in the title area of the page.

Once you have entered content in the content placeholders and code in the code behind file, set the application page as the Startup item for the project by right-clicking the page, and then clicking Set As Startup Item. When you press F5 to start debugging the page, Visual Studio will browse to the application page.

Tip

INSIDE OUT Application master page files in SharePoint 2010

A major change in SharePoint Foundation is that application pages now use a dynamic master page that can be set on a per-site basis. A code behind file was added to the SharePoint Foundation application pages to determine which site master page to use. Now application pages can take on the same look and feel as the site, whereas in Windows SharePoint Services 3.0, unless you used themes, the look and feel of application pages were defined once for the whole farm by using the file application.master. Changing this file was not supported by Microsoft. The content of the application page was also changed so that areas—content placeholders—on the application page are similar to content placeholders on content pages. Now, if an error is made to the master pages used by application pages, then SharePoint Foundation will use the v4.master page with application pages so that they can still be viewed. You can find more information at http://msdn.microsoft.com/en-us/library/ee537530.aspx.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset