共用方式為


逐步解說:將應用程式頁面新增到工作流程

本逐步解說示範如何將應用程式頁面新增至工作流程專案,以顯示衍生自工作流程的資料。 其會以逐步解說:建立具有關聯和起始表單的工作流程主題所述的專案為基礎。

本逐步解說將示範下列工作:

  • 將 ASPX 應用程式頁面新增至 SharePoint 工作流程專案。

  • 從工作流程專案取得資料並加以操作。

  • 在應用程式頁面上的資料表中顯示資料。

    注意

    在下列指示的某些 Visual Studio 使用者介面項目中,您的電腦可能會顯示不同的名稱或位置: 您所擁有的 Visual Studio 版本以及使用的設定會決定這些項目。 如需詳細資訊,請參閱將 IDE 個人化

必要條件

您需要下列元件才能完成這個逐步解說:

修改工作流程程式碼

首先,將一行程式碼新增至工作流程,以將 [結果] 資料行的值設定為費用報表的金額。 此值稍後會在費用報表摘要計算中使用。

在工作流程中設定結果資料行的值

  1. 將完成的專案從逐步解說:建立具有關聯和起始表單的工作流程主題載入至 Visual Studio。

  2. 開啟 Workflow1.csWorkflow1.vb 的程式碼 (視您的程式設計語言而定)。

  3. 將下列程式碼新增到 createTask1_MethodInvoking 方法的底部:

    createTask1_TaskProperties1.ExtendedProperties["Outcome"] =
      workflowProperties.InitiationData;
    

建立應用程式頁面

接下來,將 ASPX 表單新增至專案。 此表單會顯示從費用報表工作流程專案取得的資料。 若要這麼做,您將新增應用程式頁面。 應用程式頁面使用與其他 SharePoint 頁面相同的主版頁面,這表示其與 SharePoint 網站上的其他頁面類似。

將應用程式頁面新增到專案

  1. 選擇 ExpenseReport 專案,然後在功能表列上,選擇 [專案]>[新增項目]

  2. 在 [範本] 窗格中,選擇 [應用程式頁面] 範本,使用專案項目的預設名稱 (ApplicationPage1.aspx),然後選擇 [新增] 按鈕。

  3. 在 ApplicationPage1.aspx 的 XML 中,將 PlaceHolderMain 區段取代為以下內容:

    <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">
        </asp:Table>
    </asp:Content>
    

    此程式碼會將資料表與標題一起新增至頁面。

  4. 將標題新增至應用程式頁面,方法是將 PlaceHolderPageTitleInTitleArea 區段取代為以下內容:

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

撰寫應用程式頁面的程式碼

接下來,將程式碼新增至費用報表摘要應用程式頁面。 當您開啟頁面時,程式碼會掃描 SharePoint 中的 [工作] 清單,以取得超出配置消費限制的費用。 報表會列出每個項目以及費用的總和。

撰寫應用程式頁面的程式碼

  1. 選擇 [ApplicationPage1.aspx] 節點,然後在功能表列上選擇 [檢視]>[程式碼] 以顯示應用程式頁面背後的程式碼。

  2. 使用以下內容案取代類別頂端的 usingImport 陳述式 (視程式設計語言而定):

    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. 將下列程式碼加入 Page_Load 方法:

    try
    {
        // Reference the Tasks list on the SharePoint site.
        // Replace "TestServer" with a valid SharePoint server name.
        SPSite site = new SPSite("http://TestServer");
        SPList list = site.AllWebs[0].Lists["Tasks"];
    
        // string text = "";
        int sum = 0;
    
        Table1.Rows.Clear();
    
        // 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";
        hr.Cells.Add(hc1);
        hr.Cells.Add(hc2);
        // 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 = "";
    
            try
            {
                // Task has the fields - treat as expense report.
                s_relContent = item.GetFormattedValue("Related
                  Content");
                s_outcome = item.GetFormattedValue("Outcome");
            }
            catch
            {
                // Task does not have fields - skip it.
                continue;
            }
    
            if (!String.IsNullOrEmpty(s_relContent) &&
              !String.IsNullOrEmpty(s_outcome))
            {
                // 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();
                dataRow2.Cells.Add(relContent);
                dataRow2.Cells.Add(outcome);
                Table1.Rows.Add(dataRow2);
            }
        }
    
        // 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.
        tfr.Cells.Add(ftc1);
        tfr.Cells.Add(ftc2);
    
        // Add the TableFooterRow to the Rows
        // collection of the table.
        Table1.Rows.Add(tfr);
    }
    
    catch (Exception errx)
    {
        System.Diagnostics.Debug.WriteLine("Error: " + errx.ToString());
    }
    

    警告

    務必將程式碼中的 "TestServer" 取代為執行 SharePoint 的有效伺服器名稱。

測試應用程式頁面

接下來,判斷應用程式頁面是否正確顯示費用資料。

測試應用程式頁面

  1. 選擇 F5 鍵以執行專案並將其部署至 SharePoint。

  2. 選擇 [首頁] 按鈕,然後選擇快速啟動列上的 [共用文件] 連結,以顯示 SharePoint 網站上的 [共用文件] 清單。

  3. 若要代表此範例的費用報表,請選擇頁面頂端 [LibraryTools] 索引標籤上的 [文件] 連結,然後選擇工具功能區上的 [上傳文件] 按鈕,將一些新文件上傳至 [文件] 清單中。

  4. 上傳一些文件之後,請選擇頁面頂端 [LibraryTools] 索引標籤上的 [程式庫] 連結,然後選擇工具功能區上的 [程式庫設定] 按鈕來具現化工作流程。

  5. 在 [文件庫設定] 頁面中,選擇 [權限和管理] 區段中的 [工作流程設定] 連結。

  6. 在 [工作流程設定] 頁面中,選擇 [新增工作流程] 連結。

  7. 在 [新增工作流程] 頁面中,選擇 [ExpenseReport - Workflow1] 工作流程,輸入工作流程的名稱,例如 ExpenseTest,然後選擇 [下一步] 按鈕。

    工作流程關聯表單隨即出現。 使用該表單來報告費用限制金額。

  8. 在 [關聯] 表單中,將 1000 輸入到 [自動核准限制] 方塊中,然後選擇 [關聯工作流程] 按鈕。

  9. 選擇 [首頁] 按鈕以返回 SharePoint 首頁。

  10. 選擇快速啟動列上的 [共用文件] 連結。

  11. 選擇其中一個上傳的文件以顯示下拉式箭頭、加以選擇,然後選擇 [工作流程] 項目。

  12. 選擇 ExpenseTest 旁的影像以顯示工作流程起始表單。

  13. 在 [費用總計] 文字方塊中,輸入大於 1000 的值,然後選擇 [開始工作流程] 按鈕。

    當回報的費用超出配置的費用金額時,工作清單中會新增一項工作。 名為 ExpenseTest 且值為 [已完成] 的資料行也會新增至 [共用文件] 清單中的費用報表項目。

  14. 對 [共用文件] 清單中的其他文件重複步驟 11 - 13。 (確切的文件數目不重要。)

  15. 在網頁瀏覽器中開啟下列 URL 來顯示費用報表摘要應用程式頁面:http://SystemName/_layouts/ExpenseReport/ApplicationPage1.aspx

    費用報表摘要頁面會列出超出配置金額的所有費用報表、其超過的金額,以及所有報表的總金額。