Exercise 4: Adding Association Forms in Visual Studio 2010

In this exercise you will add an association form to the timesheet submission workflow. This form will allow workflow users to configure an hourly rate.

  1. Add a new association form to the Workflow1 workflow.
    1. Right click on the Workflow1 folder and click Add >>New Item. (Note: Make sure that you select the workflow node and not the project node because you will be able to add an association form but it will never be associated with the workflow.)
    2. Select Workflow Association Form from Common Items >> SharePoint >> 2010, name it AssociationForm.aspx and click Add.
    3. In the asp:Content control with the ID of Main, add the following markup to create the page.

      ASPX

      <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server"> <table> <tr> <td>Hourly rate:</td> <td> <asp:TextBox ID="HourlyRateTextBox" runat="server" /> </td> </tr> </table> <asp:Button ID="AssociateWorkflow" runat="server" OnClick="AssociateWorkflow_Click" Text="Associate Workflow" />
    4. Right click the markup window and choose View Code.
    5. Add the following code to GetAssociationData to save the hourly rate settings to the association data.

      C#

      private string GetAssociationData() { double rate = 0.0; double.TryParse(HourlyRateTextBox.Text, out rate); return rate.ToString(); }

      VB.NET

      Private Function GetAssociationData() As String Dim rate As Double = 0.0 Double.TryParse(HourlyRateTextBox.Text, rate) Return rate.ToString() End Function
  2. Add a new code activity to the workflow to parse the association data and calculate the amount to pay.
    1. Open Workflow1 in the designer.
    2. Drag a new code activity from the toolbox and drop it between the onWorkflowActivated1 activity and the SubmitToHR activity.
    3. Rename the activity to InitializeAssocData.

      Figure 1

      Design the workflow activities in Visual Studio 2010

    4. Double-click the InitializeAssocData activity to generate the event handler.
    5. Add the following code to retrieve the association data from the association form and to calculate the due amount based on the hours filled out on the time sheet and the hourly rate specified in the association form.

      C#

      private void InitizalizeAssocData_ExecuteCode(object sender,EventArgs e) { // retrieve the association data from the association form OnWorkflowActivated activity = this.Activities["onWorkflowActivated1"] as OnWorkflowActivated; string associationData = activity.WorkflowProperties.AssociationData; // retrieve the hourly rate from the association data double.TryParse(associationData, out amountToPay); // retrieve the list item SPListItem item = workflowProperties.Item; // get the hours on the timesheet int hours = 0; int.TryParse(item["Hours"].ToString(), out hours); // calculate and save the amount to pay item["Payment Amount"] = amountToPay * hours; item.Update(); }

      VB.NET

      Private Sub InitializeAssocData_ExecuteCode(ByVal sender As stem.Object, ByVal e As System.EventArgs) ' retrieve the association data from the association form Dim activity As OnWorkflowActivated = TryCast(Me.Activities("onWorkflowActivated1"), OnWorkflowActivated) Dim associationData As String = activity.WorkflowProperties.AssociationData ' retrieve the hourly rate from the association data Dim amountToPay as Double Double.TryParse(associationData, amountToPay) ' retrieve the list item Dim item As SPListItem = workflowProperties.Item ' get the hours on the timesheet Dim hours As Integer = 0 Integer.TryParse(item("Hours").ToString(), hours) ' calculate and save the amount to pay item("Payment Amount") = amountToPay * hours item.Update() End Sub
  3. Deploy the workflow to SharePoint by right clicking the project in solution explorer and clicking Deploy.
  4. Use the new association page to fill out an hourly rate.
    1. Open Internet Explorer and navigate to http://intranet.contoso.com/sites/Lab07/Lists/TimeSheets.
    2. Click Library Settings in the Library ribbon tab.
    3. Click the Workflow Settings link in the Permissions and Management section.
    4. Click the Timesheet Submission VS link to manage the workflow association.
    5. Click Next to view the custom association page.
    6. Enter $500 for Hourly rate: and click Associate Workflow.

      Figure 2

      The association page

  5. Navigate back to http://intranet.contoso.com/sites/Lab07/Lists/TimeSheets and start the workflow.
    1. Use the drop down menu on a timesheet and the Edit Properties link to change the hours to 35.
    2. Click the drop down menu on this same item again, and select Workflows.
    3. Click the Timesheet Submission VS link to start the workflow.
    4. When the workflow is complete, you should see that a payment amount is calculated.
Note:
In this exercise you added an association form to the Visual Studio 2010 workflow you created in the previous exercise and tested the new settings.