Walkthrough: Creating an Application Page

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

This walkthrough shows you how to create an application page and then debug that page by using a local SharePoint site. This application page enables the current user to view all items that they have 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.

Not

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 Working with Settings.

Prerequisites

You need the following components to complete this walkthrough:

Creating a SharePoint Project

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

To create a SharePoint Project

  1. Start Visual Studio 2010.

  2. Open the New Project dialog box, expand the SharePoint node under the language that you want to use, and then click 2010.

  3. In the Visual Studio Installed Templates pane, select Empty SharePoint Project. Name the project MySharePointProject, and then click OK.

    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. Select Deploy as a farm solution, and then click Finish to accept the default local SharePoint site.

Creating an Application Page

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

To create an application page

  1. In Solution Explorer, select the MySharePointProject project.

  2. On the Project menu, click Add New Item.

  3. In the Add New Item dialog box, select Application Page.

  4. Name the page SearchItems, and then click Add.

    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.

Designing 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. Drag a label, a radio button list, and a table to the Source view of the designer and set properties just as you would design any standard ASP.NET page.

For more information about using the designer in Visual Web Developer, see Visual Web Developer Content Map.

To design the layout of the application page

  1. On the View menu, click Toolbox.

  2. In the Toolbox, from the Standard group, drag a Label, a DropDownList, and a Table onto the body of the PlaceHolderMain content control.

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

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

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

Handling 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.

To handle the events of controls on the page

  1. On the View menu, click 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)
    {
        SPSecurity.RunWithElevatedPrivileges(GetItems);
    
    }
    
  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
                        Try
                            For Each Item In List.Items
                                If Item(DropDownList1.SelectedValue).ToString() = _
                                    CurrentUser.ToString() Then
                                    ResultsList.Add(Item)
                                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
        CreateResultsTable(ResultsList)
    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)
                    {
                        try
                        {
                            foreach (SPListItem item in list.Items)
                            {
                                if (item[DropDownList1.SelectedValue].ToString() ==
                                    currentUser.ToString())
                                {
                                    resultsList.Add(item);
                                }
                            }
                        }
                        catch (Exception)
                        {
                            // An error with the list. Move onto the next list.
                        }
                    }
                }
            }
        }
        CreateResultsTable(resultsList);
    
    }
    
  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 = ""
        Table1.Rows.Clear()
    
        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
                    .Cells.Add(NewSiteCell)
                    .Font.Bold = True
                    .Font.Size = FontUnit.Larger
                    .Font.Underline = True
                End With
                Table1.Rows.Add(NewSiteRow)
    
            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
                    .Cells.Add(NewListCell)
                    .Font.Bold = True
                End With
                Table1.Rows.Add(NewListRow)
    
                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()
                HeadingRow.Cells.Add(ItemHeading)
                HeadingRow.Cells.Add(CreatedHeading)
                HeadingRow.Cells.Add(ModifiedHeading)
    
                Table1.Rows.Add(HeadingRow)
            End If
    
            Dim ItemName As New TableCell()
            Dim ItemLink As New HyperLink()
            Try
                ItemLink.href = 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
            ItemName.Controls.Add(ItemLink)
    
            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()
            DataRow.Cells.Add(ItemName)
            DataRow.Cells.Add(Created)
            DataRow.Cells.Add(Modified)
    
            Table1.Rows.Add(DataRow)
        Next Item
    End Sub
    
    private void CreateResultsTable(ArrayList resultsList)
    {
        string currentList = "";
        string currentSite = "";
        Table1.Rows.Clear();
    
        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.Cells.Add(newSiteCell);
                newSiteRow.Font.Bold = true;
                newSiteRow.Font.Size = FontUnit.Larger;
                newSiteRow.Font.Underline = true;
    
                Table1.Rows.Add(newSiteRow);
            }
            if (item.ParentList.Title != currentList)
            {
                currentList = item.ParentList.Title;
                TableCell newListCell = new TableCell();
                newListCell.Text = currentList;
    
                TableRow newListRow = new TableRow();
                newListRow.Cells.Add(newListCell);
                newListRow.Font.Bold = true;
    
                Table1.Rows.Add(newListRow);
    
                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();
                headingRow.Cells.Add(itemHeading);
                headingRow.Cells.Add(createdHeading);
                headingRow.Cells.Add(modifiedHeading);
    
                Table1.Rows.Add(headingRow);
            }
    
            TableCell itemName = new TableCell();
            HyperLink itemLink = new HyperLink();
            try
            {
                itemLink.href = 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;
            itemName.Controls.Add(itemLink);
    
            TableCell created = new TableCell();
            created.Text = item["Created"].ToString();
    
            TableCell modified = new TableCell();
            modified.Text = item["Modified"].ToString();
    
            TableRow dataRow = new TableRow();
            dataRow.Cells.Add(itemName);
            dataRow.Cells.Add(created);
            dataRow.Cells.Add(modified);
    
            Table1.Rows.Add(dataRow);
        }
    }
    

Testing the Application Page

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

To test the application page

  1. In Solution Explorer, right-click the application page, and then click Set as Startup Item.

  2. Press F5.

    The SharePoint site opens. The application page appears.

  3. On the page, click the Modified by me option.

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

  4. On the page, click the Created by me option.

    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 Creating 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

Tasks

How to: Create an Application Page

Other Resources

Application _layouts Page Type