Edit

Share via


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.

Note

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.

Prerequisites

  • 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"
     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 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 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);
        }
    }
    

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: