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

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

このチュートリアルでは、次のタスクを実行します。

  • 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 プロジェクトを、メニュー バーのをクリックし、プロジェクト新しい項目の追加を選択します。

  2. テンプレート のペインで、アプリケーション ページ テンプレートを使用し、既定の名前をプロジェクト項目 (ApplicaitonPage1.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. プログラミング言語に応じて、クラスの冒頭の 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://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());
    }
    
    Caution メモ注意

    連続した SharePoint の有効なサーバーの名前に 「」のコード TestServer を置き換えることを確認します。

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

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

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

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

  2. ホーム のボタンを選択し、共有ドキュメントを表示するためのクイック起動バーのリンクを示します 共有ドキュメント の SharePoint サイトのをクリックします。

  3. この例の経費報告書を表すには、ページ上部のタブの [LibraryTools]ドキュメント のリンクを選択し、ツール リボンの ドキュメントのアップロード のボタンを選択することによって、ドキュメント リストにある新しいドキュメントをアップロードします。

  4. あるドキュメントをアップロードしたら、ページの上部に [LibraryTools] のタブの library のリンクを選択し、ツール リボンの ライブラリの設定 のボタンを選択することによって、ワークフローをインスタンス化します。

  5. [ドキュメント ライブラリの設定] のページで、権限と管理 のセクションの ワークフロー設定 のリンクを選択します。

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

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

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

  8. 関連付けフォームでは、1000 に [自動承認の制限] ボックスに " " と入力し、ワークフローの関連付け のボタンをクリックします。

  9. SharePoint のホーム ページに戻るに ホーム のボタンをクリックします。

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

  11. ドロップダウン矢印を表示するためにアップロードしたドキュメントの 1 種類を選択し、を選択し、を ワークフロー の項目を選択します。

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

  13. 経費の合計 のテキスト ボックスに、1000 より大きい入力し、を ワークフローの開始 のボタンを選択します。値を取得します。

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

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

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

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

次の手順

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

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

参照

処理手順

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

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

その他の技術情報

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

SharePoint ソリューションの開発