Walkthrough: Add an Application Page to a Workflow

This walkthrough demonstrates how to add an application page that displays data derived from a workflow to a workflow project. It builds on the project described in the topic Walkthrough: Creating a Workflow with Association and Initiation Forms.

This walkthrough demonstrates the following tasks:

  • Adding an ASPX application page to a SharePoint workflow project.

  • Obtaining data from the workflow project and manipulating it.

  • Displaying data in a table on 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 Visual Studio Settings.

You need the following components to complete this walkthrough:

First, add a line of code to the workflow to set the value of the Outcome column to the amount of the expense report. This value is used later in the expense report summary calculation.

To set the value of the Outcome column in the workflow

  1. Load the completed project from the topic Walkthrough: Creating a Workflow with Association and Initiation Forms into Visual Studio.

  2. Open the code for Workflow1.cs or Workflow1.vb (depending on your programming language).

  3. To the bottom of the createTask1_MethodInvoking method, add the following code:

    createTask1_TaskProperties1.ExtendedProperties["Outcome"] = 

Next, add an ASPX form to the project. This form will display data obtained from the expense report workflow project. To do this, you will add an application page. An application page uses the same master page as other SharePoint pages, meaning that it will resemble other pages on the SharePoint site.

To add an application page to the project

  1. Add an application page to the project. Right-click the ExpenseReport project, point to Add, and then click New Item. Use the default name for the project item: ApplicationPage1.aspx.

  2. In the XML of ApplicationPage1.aspx, replace the PlaceHolderMain section with the following:

    <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
        <asp:Label ID="Label1" runat="server" Font-Bold="True" 
            Text="Expenses that exceeded allotted amount" Font-Size="Medium"></asp:Label>
        <br />
        <asp:Table ID="Table1" runat="server">

    This code adds a table to the page together with a title.

  3. Add a title to the application page by replacing the PlaceHolderPageTitleInTitleArea section with the following:

    <asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
        Expense Report Summary

Next, add code to the expense report summary application page. When you open the page, the code scans the Task list in SharePoint for expenses that exceeded the allocated spending limit. The report lists each item together with the sum of the expenses.

To code the application page

  1. Click ApplicationPage1.aspx and then click Code on the View menu to display the code behind the application page.

  2. Replace the using or Import statements (depending on your programming language) at the top of the class with the following:

    using System;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using System.Collections;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Drawing;
    using Microsoft.SharePoint.Navigation;
  3. Add the following code to the Page_Load method:

        // Reference the Tasks list on the SharePoint site.
        // Replace "TestServer" with a valid SharePoint server name.
        SPSite site = new SPSite("http://bauetest");
        SPList list = site.AllWebs[0].Lists["Tasks"];
        // string text = "";
        int sum = 0;
        // Add table headers.
        TableHeaderRow hr = new TableHeaderRow();
        hr.BackColor = Color.LightBlue;
        TableHeaderCell hc1 = new TableHeaderCell();
        TableHeaderCell hc2 = new TableHeaderCell();
        hc1.Text = "Expense Report Name";
        hc2.Text = "Amount Exceeded";
        // Add the TableHeaderRow as the first item 
        // in the Rows collection of the table.
        Table1.Rows.AddAt(0, hr);
        // Iterate through the tasks in the Task list and collect those 
        // that have values in the "Related Content" and "Outcome" 
        // fields - the fields written to when expense approval is 
        // required.
        foreach (SPListItem item in list.Items)
            string s_relContent = "";
            string s_outcome = "";
                // Task has the fields - treat as expense report.
                s_relContent = item.GetFormattedValue("Related 
                s_outcome = item.GetFormattedValue("Outcome");
                // Task does not have fields - skip it.
            if (!String.IsNullOrEmpty(s_relContent) && 
                // Convert amount to an int and keep a running total.
                sum += Convert.ToInt32(s_outcome);
                TableCell relContent = new TableCell();
                relContent.Text = s_relContent;
                TableCell outcome = new TableCell();
                outcome.Text = "$" + s_outcome;
                TableRow dataRow2 = new TableRow();
        // Report the sum of the reports in the table footer.
           TableFooterRow tfr = new TableFooterRow();
        tfr.BackColor = Color.LightGreen;
        // Create a TableCell object to contain the 
        // text for the footer.
        TableCell ftc1 = new TableCell();
        TableCell ftc2 = new TableCell();
        ftc1.Text = "TOTAL: ";
        ftc2.Text = "$" + Convert.ToString(sum);
        // Add the TableCell object to the Cells
        // collection of the TableFooterRow.
        // Add the TableFooterRow to the Rows
        // collection of the table.
    catch (Exception errx)
        System.Diagnostics.Debug.WriteLine("Error: " + errx.ToString());

Next, determine whether the application page displays the expense data correctly.

To test the application page

  1. Press F5 to run and deploy the project to SharePoint.

  2. Click the Home button and then display the Shared Documents list on the SharePoint site by clicking the Shared Documents link on the QuickLaunch bar.

  3. To represent expense reports for this example, upload some new documents into the Documents list by clicking the Documents link on the LibraryTools tab at the top of the page, and then clicking the Upload Document button on the tool ribbon.

  4. After you upload some documents, instantiate the workflow. Click Library on the LibraryTools tab at the top of the page and then click the Library Settings button on the tool ribbon.

  5. In the Shared Documents: List Settings page, click the Workflow Settings link in the Permissions and Management section.

  6. In the Workflow Settings page, click the Add a workflow link.

  7. In the Add a Workflow page, select the ExpenseReport - Workflow1 workflow, enter a name for the workflow, such as ExpenseTest, and then click Next.

    The workflow Association form appears. Use it to report the expense limit amount.

  8. In the Association form, enter 1000 into the Auto Approval Limit box.

  9. Click the Home button to return to the SharePoint home page.

  10. Click the Shared Documents link on the QuickLaunch bar.

  11. Point the mouse at one of the uploaded documents to display a drop-down arrow. Click the drop-down arrow and select Workflows.

  12. Click the image next to the ExpenseTest to display the workflow Initiation form.

  13. In the Expense Total text box, enter a value greater than 1000 and then click Start Workflow.

    When a reported expense exceeds the allocated expense amount, a task is added to the Task List. A column named ExpenseTest with the value Completed is also added to the expense report item in the Shared Documents list.

  14. Repeat steps 11 - 13 with other documents in the Shared Documents list. (The exact number of documents is not important.)

  15. Close the browser to stop the program.

  16. Display the expense report summary application page by opening the following URL in a Web browser: http://SystemName/_layouts/ExpenseReport/ApplicationPage1.aspx.

    The expense report summary page lists all of the expense reports that exceeded the allocated amount, the amount they exceeded it by, and the total amount for all reports.

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 in Visual Studio from these topics:

Community Additions