Walkthrough: Create a SharePoint application page

An application page is a specialized form of an ASP.NET page. Application pages contain content that's merged with a SharePoint master page. For more information, see Create application pages for SharePoint.

This walkthrough shows you how to create an application page and then debug it by using a local SharePoint site. This page shows all items that each user has created or modified in all sites on the server farm.

This walkthrough illustrates the following tasks:

  • Creating a SharePoint project.
  • Adding an application page to the SharePoint project.
  • Adding ASP.NET controls to the application page.
  • Adding code behind the ASP.NET controls.
  • Testing the application page.


Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalize the Visual Studio IDE.


  • Supported editions of Windows and SharePoint.

Create a SharePoint project

First, create an Empty SharePoint Project. Later, you will add an Application Page item to this project.

  1. Start Visual Studio.

  2. Open the New Project dialog box, expand the Office/SharePoint node under the language that you want to use, and then choose the SharePoint Solutions node.

  3. In the Visual Studio Installed Templates pane, choose the SharePoint 2010 - Empty Project template. Name the project MySharePointProject, and then choose the OK button.

    The SharePoint Customization Wizard appears. This wizard enables you to select the site that you will use to debug the project and the trust level of the solution.

  4. Choose the Deploy as a farm solution option button, and then choose the Finish button to accept the default local SharePoint site.

Create an application page

To create an application page, add an Application Page item to the project.

  1. In Solution Explorer, choose the MySharePointProject project.

  2. On the menu bar, choose Project > Add New Item.

  3. In the Add New Item dialog box, choose the Application Page (Farm Solution Only template.

  4. Name the page SearchItems, and then choose the Add button.

    The Visual Web Developer designer displays the application page in Source view where you can see the page's HTML elements. The designer displays the markup for several Content controls. Each control maps to a ContentPlaceHolder control that is defined in the default application master page.

Design the layout of the application page

The Application Page item enables you to use a designer to add ASP.NET controls to the application page. This designer is the same designer used in Visual Web Developer. Add a label, a radio button list, and a table to the Source view of the designer, and then set properties just as you would when you design any standard ASP.NET page.

  1. On the menu bar, choose View > Toolbox.

  2. In the Standard node of the Toolbox, perform one of the following steps:

    • Open the shortcut menu for the Label item, choose Copy, open the shortcut menu for the line under the PlaceHolderMain content control in the designer, and then choose Paste.

    • Drag the Label item from the Toolbox onto the body of the PlaceHolderMain content control.

  3. Repeat the previous step to add a DropDownList item and a Table item to the PlaceHolderMain content control.

  4. On the designer, change the value of the Text attribute of the label control to Show All Items.

  5. On the designer, replace the <asp:DropDownList> element with the following XML.

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
        <asp:ListItem Text="Created by me" Value="Author"></asp:ListItem>
        <asp:ListItem Text="Modified by me" Value="Editor"></asp:ListItem>

Handle the events of controls on the page

Handle controls in an application page just as you would any ASP.NET page. In this procedure, you will handle the SelectedIndexChanged event of the drop-down list.

  1. On the View menu, choose Code.

    The application page code file opens in the Code Editor.

  2. Add the following method to the SearchItems class. This code handles the SelectedIndexChanged event of the DropDownList by calling a method that you will create later in this walkthrough.

     Protected Sub DropDownList1_SelectedIndexChanged _
    (ByVal sender As Object, ByVal e As EventArgs) _
    Handles DropDownList1.SelectedIndexChanged
         SPSecurity.RunWithElevatedPrivileges(AddressOf GetItems)
     End Sub
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  3. Add the following statements to the top of the application page code file.

    Imports System.Web.UI.WebControls
    Imports Microsoft.SharePoint.Administration
    Imports System.Collections
    using System.Web.UI.WebControls;
    using Microsoft.SharePoint.Administration;
    using System.Collections;
  4. Add the following method to the SearchItems class. This method iterates through all sites on the server farm and searches for items created or modified by the current user.

    Private Sub GetItems()
        Dim CurrentUser As New SPFieldUserValue _
            (Me.Web, Me.Web.CurrentUser.ID, Me.Web.CurrentUser.Name)
        Dim ResultsList As New ArrayList()
        Dim ThisFarm As SPFarm = SPFarm.Local
        Dim Service As SPWebService = _
            ThisFarm.Services.GetValue(Of SPWebService)("")
        Dim WebApp As SPWebApplication
        For Each WebApp In Service.WebApplications
            Dim SiteCollection As SPSite
            For Each SiteCollection In WebApp.Sites
                Dim Web As SPWeb
                For Each Web In SiteCollection.AllWebs
                    Dim Lists As SPListCollection = Web.Lists
                    Dim List As SPList
                    For Each List In Lists
                        Dim Item As SPListItem
                            For Each Item In List.Items
                                If Item(DropDownList1.SelectedValue).ToString() = _
                                    CurrentUser.ToString() Then
                                End If
                            Next Item
                        Catch exception As Exception
                            'Error in list. Move on to the next list.
                        End Try
                    Next List
                Next Web
            Next SiteCollection
        Next WebApp
    End Sub
    private void GetItems()
         SPFieldUserValue currentUser = new SPFieldUserValue
         (this.Web, this.Web.CurrentUser.ID, this.Web.CurrentUser.Name);
        ArrayList resultsList = new ArrayList();
        SPFarm thisFarm = SPFarm.Local;
        SPWebService service = thisFarm.Services.GetValue<SPWebService>("");
        foreach (SPWebApplication webApp in service.WebApplications)
            foreach (SPSite siteCollection in webApp.Sites)
                foreach (SPWeb web in siteCollection.AllWebs)
                    SPListCollection lists = web.Lists;
                    foreach (SPList list in lists)
                            foreach (SPListItem item in list.Items)
                                if (item[DropDownList1.SelectedValue].ToString() ==
                        catch (Exception)
                            // An error with the list. Move onto the next list.
  5. Add the following method to the SearchItems class. This method displays items created or modified by the current user in the table.

    Private Sub CreateResultsTable(ByVal ResultsList As ArrayList)
        Dim CurrentList As String = ""
        Dim CurrentSite As String = ""
        Dim Item As SPListItem
        For Each Item In ResultsList
            If Item.ParentList.ParentWeb.Title <> CurrentSite Then
                CurrentSite = Item.ParentList.ParentWeb.Title
                Dim NewSiteCell As New TableCell()
                NewSiteCell.Text = CurrentSite
                Dim NewSiteRow As New TableRow()
                With NewSiteRow
                    .Font.Bold = True
                    .Font.Size = FontUnit.Larger
                    .Font.Underline = True
                End With
            End If
            If Item.ParentList.Title <> CurrentList Then
                CurrentList = Item.ParentList.Title
                Dim NewListCell As New TableCell()
                NewListCell.Text = CurrentList
                Dim NewListRow As New TableRow()
                With NewListRow
                    .Font.Bold = True
                End With
                Dim ItemHeading As New TableCell()
                With ItemHeading
                    .Text = "Item"
                    .Font.Italic = True
                End With
                Dim CreatedHeading As New TableCell()
                With CreatedHeading
                    .Text = "Created"
                    .Font.Italic = True
                End With
                Dim ModifiedHeading As New TableCell()
                With ModifiedHeading
                    .Text = "Last Modified"
                    .Font.Italic = True
                End With
                Dim HeadingRow As New TableRow()
            End If
            Dim ItemName As New TableCell()
            Dim ItemLink As New HyperLink()
                ItemLink.NavigateUrl = Item.ParentList.ParentWeb.Url & _
                    "/" & Item.ParentList.Forms(PAGETYPE.PAGE_DISPLAYFORM).Url & _
                    "?ID=" & Item.ID
            Catch exception As Exception
                ' Some items might not have a form page. Ignore the exception.
            End Try
            ItemLink.Text = Item.DisplayName
            Dim Created As New TableCell()
            Created.Text = Item("Created").ToString()
            Dim Modified As New TableCell()
            Modified.Text = Item("Modified").ToString()
            Dim DataRow As New TableRow()
        Next Item
    End Sub
    private void CreateResultsTable(ArrayList resultsList)
        string currentList = "";
        string currentSite = "";
        foreach (SPListItem item in resultsList)
            if (item.ParentList.ParentWeb.Title != currentSite)
                currentSite = item.ParentList.ParentWeb.Title;
                TableCell newSiteCell = new TableCell();
                newSiteCell.Text = currentSite;
                TableRow newSiteRow = new TableRow();
                newSiteRow.Font.Bold = true;
                newSiteRow.Font.Size = FontUnit.Larger;
                newSiteRow.Font.Underline = true;
            if (item.ParentList.Title != currentList)
                currentList = item.ParentList.Title;
                TableCell newListCell = new TableCell();
                newListCell.Text = currentList;
                TableRow newListRow = new TableRow();
                newListRow.Font.Bold = true;
                TableCell itemHeading = new TableCell();
                itemHeading.Text = "Item";
                itemHeading.Font.Italic = true;
                TableCell createdHeading = new TableCell();
                createdHeading.Text = "Created";
                createdHeading.Font.Italic = true;
                TableCell modifiedHeading = new TableCell();
                modifiedHeading.Text = "Last Modified";
                modifiedHeading.Font.Italic = true;
                TableRow headingRow = new TableRow();
            TableCell itemName = new TableCell();
            HyperLink itemLink = new HyperLink();
                itemLink.NavigateUrl = item.ParentList.ParentWeb.Url + "/" +
                    item.ParentList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url +
                    "?ID=" + item.ID;
            catch (Exception)
                // Some items might not have a form page. Ignore the exception.
            itemLink.Text = item.DisplayName;
            TableCell created = new TableCell();
            created.Text = item["Created"].ToString();
            TableCell modified = new TableCell();
            modified.Text = item["Modified"].ToString();
            TableRow dataRow = new TableRow();

Test the application page

When you run the project, the SharePoint site opens and the application page appears.

  1. In Solution Explorer, open the shortcut menu for the application page, and then choose Set as Startup Item.

  2. Choose the F5 key.

    The SharePoint site opens.

  3. On the application page, choose the Modified by me option.

    The application page refreshes and displays all items that you've modified in all sites on the server farm.

  4. On the application page, choose Created by me in the list.

    The application page refreshes and displays all items that you have created in all sites on the server farm.

Next steps

For more information about SharePoint application pages, see Create application pages for SharePoint.

You can learn more about how to design SharePoint page content by using the Visual Web Designer from these topics:

See also

How to: Create an application page Application _layouts Page Type