チュートリアル: ワークフローへのアプリケーション ページの追加

このチュートリアルでは、ワークフローから得られたデータを表示するためのアプリケーション ページをワークフロー プロジェクトに追加する方法について説明します。 「チュートリアル: 関連付けフォームと開始フォームを持つワークフローの作成」のトピックで取り上げたプロジェクトがベースとなります。

このチュートリアルでは、次のタスクについて説明します。

  • SharePoint ワークフロー プロジェクトに ASPX アプリケーション ページを追加する。

  • ワークフロー プロジェクトからデータを取得して操作する。

  • アプリケーション ページ上のテーブルにデータを表示する。

注意

お使いのマシンで、Visual Studio ユーザー インターフェイスの一部の要素の名前や場所が、次の手順とは異なる場合があります。 これらの要素は、使用している Visual Studio のエディションや独自の設定によって決まります。 詳細については、「Visual Studio の設定」を参照してください。

必須コンポーネント

このチュートリアルを実行するには、次のコンポーネントが必要です。

ワークフロー コードの修正

まず、Outcome 列の値を経費明細書の金額に設定するコード行をワークフローに追加します。 この値は、後で経費明細書の概要 (Expense Report Summary) の計算に使用します。

ワークフローで Outcome 列の値を設定するには

  1. チュートリアル: 関連付けフォームと開始フォームを持つワークフローの作成」で作成したプロジェクトを Visual Studio に読み込みます。

  2. プログラミング言語に応じて Workflow1.cs または Workflow1.vb のコードを開きます。

  3. createTask1_MethodInvoking メソッドの最後に、次のコードを追加します。

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

アプリケーション ページの作成

次に、ASPX フォームをプロジェクトに追加します。 このフォームには、経費明細書ワークフロー プロジェクトから取得されたデータを表示します。 ここでは、そのためのアプリケーション ページを追加します。 アプリケーション ページには、他の SharePoint ページと同じマスター ページを使用します。つまり、外見上は SharePoint サイトの他のページと似ています。

プロジェクトにアプリケーション ページを追加するには

  1. プロジェクトにアプリケーション ページを追加します。 ExpenseReport プロジェクト ノードを右クリックし、[追加] をポイントして、[新しい項目] をクリックします。 プロジェクト項目には、既定の名前である ApplicationPage1.aspx を使用します。

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

    このコードによって、ページにテーブルとタイトルが追加されます。

  3. PlaceHolderPageTitleInTitleArea セクションを次のように置き換えて、アプリケーション ページにタイトルを追加します。

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

アプリケーション ページのコーディング

次に、経費明細書の概要を表示するアプリケーション ページにコードを追加します。 ページを開くと、SharePoint 内のタスク リストがスキャンされ、割り当てられた支出制限を超える経費がないかどうかが調べられます。 経費明細書には、個々の項目と経費の合計が一覧表示されます。

アプリケーション ページをコーディングするには

  1. [ApplicationPage1.aspx] をクリックし、[表示] メニューの [コード] をクリックして、アプリケーション ページの分離コードを表示します。

  2. プログラミング言語に応じて、クラスの冒頭の using ステートメントまたは Import ステートメントを次のように置き換えます。

    Imports System
    Imports Microsoft.SharePoint
    Imports Microsoft.SharePoint.WebControls
    Imports System.Collections
    Imports System.Data
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.WebControls.WebParts
    Imports System.Drawing
    Imports Microsoft.SharePoint.Navigation
    
    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.
        Dim site As SPSite = New SPSite("http://TestServer")
        Dim list As SPList = site.AllWebs(0).Lists("Tasks")
        ' string text = "";
        Dim sum As Integer = 0
        Table1.Rows.Clear()
        ' Add table headers.
        Dim hr As TableHeaderRow = New TableHeaderRow
        hr.BackColor = Color.LightBlue
        Dim hc1 As TableHeaderCell = New TableHeaderCell
        Dim hc2 As TableHeaderCell = 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.
        For Each item As SPListItem In list.Items
            Dim s_relContent As String = ""
            Dim s_outcome As String = ""
            Try
                ' Task has the fields - treat as expense report.
                s_relContent = item.GetFormattedValue("Related Content")
                s_outcome = item.GetFormattedValue("Outcome")
            Catch erx As System.Exception
                ' Task does not have fields - skip it.
                Continue For
            End Try
            ' Convert amount to an int and keep a running total.
            If (Not String.IsNullOrEmpty(s_relContent) And Not 
              String.IsNullOrEmpty(s_outcome)) Then
                sum = (sum + Convert.ToInt32(s_outcome))
                Dim relContent As TableCell = New TableCell
                relContent.Text = s_relContent
                Dim outcome As TableCell = New TableCell
                outcome.Text = ("$" + s_outcome)
                Dim dataRow2 As TableRow = New TableRow
                dataRow2.Cells.Add(relContent)
                dataRow2.Cells.Add(outcome)
                Table1.Rows.Add(dataRow2)
            End If
        Next
        ' Report the sum of the reports in the table footer.
        Dim tfr As TableFooterRow = New TableFooterRow
        tfr.BackColor = Color.LightGreen
        ' Create a TableCell object to contain the 
        ' text for the footer.
        Dim ftc1 As TableCell = New TableCell
        Dim ftc2 As TableCell = 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 errx As Exception
        System.Diagnostics.Debug.WriteLine(("Error: " + errx.ToString))
    End Try
    
    try
    {
        // 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;
    
        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());
    }
    

アプリケーション ページのテスト

次に、アプリケーション ページに経費データが正しく表示されるかどうかを確認します。

アプリケーション ページをテストするには

  1. F5 キーを押して、プロジェクトを実行し、SharePoint に配置します。

  2. [ホーム] ボタンをクリックし、クイック起動バーの [共有ドキュメント] リンクをクリックして、SharePoint サイト上の共有ドキュメント リストを表示します。

  3. この例で経費明細書を表現するには、ページ上部の [ライブラリ ツール] タブの [ドキュメント] リンクをクリックし、ツール リボンの [ドキュメントのアップロード] ボタンをクリックして、ドキュメント リストに新しいドキュメントをいくつかアップロードします。

  4. いくつかのドキュメントをアップロードしたら、ワークフローをインスタンス化します。 ページ上部の [ライブラリ ツール] タブの [ライブラリ] をクリックし、ツール リボンの [ライブラリの設定] ボタンをクリックします。

  5. [共有ドキュメント: リストの設定] ページで、[権限と管理] セクションの [ワークフロー設定] リンクをクリックします。

  6. [ワークフロー設定] ページで、[ワークフローの追加] リンクをクリックします。

  7. [ワークフローの追加] ページで、[ExpenseReport - Workflow1] ワークフローを選択し、ワークフローの名前 (「ExpenseTest」など) を入力して、[次へ] をクリックします。

    ワークフローの関連付けフォームが表示されます。 ここで経費の上限金額を記録します。

  8. 関連付けフォームの [Auto Approval Limit] ボックスに「1000」と入力します。

  9. [ホーム] ボタンをクリックして、SharePoint のホーム ページに戻ります。

  10. クイック起動バーの [共有ドキュメント] リンクをクリックします。

  11. アップロードしたドキュメントのいずれかをマウスでポイントすると、ドロップダウン矢印が表示されます。 ドロップダウン矢印をクリックして [ワークフロー] をクリックします。

  12. ExpenseTest の横に表示されるイメージをクリックして、ワークフローの開始フォームを表示します。

  13. [Expense Total] ボックスに、1000 を超える値を入力し、[ワークフローの開始] をクリックします。

    報告された経費が、割り当てられている経費金額を超えると、タスク リストにタスクが追加されます。 また、共有ドキュメント リストの経費明細書項目に、ExpenseTest という列が追加され、列の値は [完了] となります。

  14. 共有ドキュメント リスト内の他のドキュメントについても手順 11. ~ 13. を繰り返します。 ドキュメントの正確な数は重要ではありません。

  15. ブラウザーを閉じて、プログラムを停止します。

  16. Web ブラウザーで "http://<システム名>/_layouts/ExpenseReport/ApplicationPage1.aspx" という URL を開き、経費明細書の概要のアプリケーション ページを表示します。

    このページには、割り当て金額を超えたすべての経費明細書と超過金額、およびすべての経費明細書の合計金額が表示されます。

次の手順

SharePoint のアプリケーション ページの詳細については、「SharePoint のアプリケーション ページの作成」を参照してください。

Visual Studio の Visual Web Designer を使用して、SharePoint ページの内容をデザインする方法の詳細については、以下のトピックを参照してください。

参照

処理手順

チュートリアル: 関連付けフォームと開始フォームを持つワークフローの作成

方法: アプリケーション ページを作成する

概念

SharePoint のアプリケーション ページの作成

その他の技術情報

SharePoint ソリューションの開発