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.

NoteNote

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.

You need the following components to complete this walkthrough:

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.

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.

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>
    

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 void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SPSecurity.RunWithElevatedPrivileges(GetItems);
    
    }
    
    
    
  3. Add the following statements to the top of the application page code file.

    
    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 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 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.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;
            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);
        }
    }
    
    
    

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.

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:

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft