Exercise 3: Developing Workflows with Visual Studio 2010

In this exercise you will build a workflow in Visual Studio 2010 that submits the timesheet information to the back end HR system and places the payment reference back into the original time sheet.

  1. Open Visual Studio 2010 and create a new workflow project:
    1. Select the Sequential Workflow template in the Visual C#/Visual Basic » SharePoint » 2010 templates folder and give it the name VSSubmissionWorkflow.
    2. Verify the URL. The workflow should be deployed to https://intranet.contoso.com/sites/Lab07. Indicate that the workflow must be deployed as a farm solution.
    3. In the wizard set the workflow name to Timesheet Submission VS and select the List Workflow radio button. Click the Next button.

      Figure 1

      The SharePoint Customization wizard in Visual Studio 2010 SharePoint Tools

    4. Clear the check box indicating a workflow association should be created on deployment. An association will be created manually to test the association page.

      Figure 2

      The SharePoint Customization Wizard

    5. Click the Finish button.
  2. Add a new Code Activity from the Windows Workflow v3.0 section of the toolbox.
  3. Add it immediately following the onWorkflowActivated1 activity.
  4. Set the name of the new code activity to SubmitToHR using the Properties window.
  5. Double-click the activity to generate the ExecuteCode event handler.
  6. Add the following code to simulate the calculation of a payment reference using a Guid, to be sure to have a unique reference. Save this reference in the time sheet on which the workflow runs.

    C#

    private void SubmitToHR_ExecuteCode(object sender, EventArgs e) { SPListItem item = workflowProperties.Item; item["Payment Ref"] = Guid.NewGuid().ToString(); item.Update(); }

    VB.NET

    Private Sub SubmitToHR_ExecuteCode(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim item As SPListItem = workflowProperties.Item item("Payment Ref") = Guid.NewGuid().ToString() item.Update() End Sub
  7. At this time you are going to prepare and deploy the workflow
  8. Setup the auto association parameters that will tell Visual Studio to automatically associate your workflow with the Time Sheets list when it is deployed.
    1. Select Workflow1 in the solution explorer.
    2. In the Properties window, set the Auto Associate property to True.
    3. In the History List property click the [...] button to start the wizard.
    4. Click Next >, On the 2nd page select a list of Time Sheets in the first dropdown and click Next >.

      Figure 3

      The SharePoint Customization Wizard

    5. On the final page check the first check box and clear the rest and click Finish.
  9. Deploy the workflow to SharePoint and verify that it works.

    1. Right click the project in solution explorer and click Deploy.
    2. When the deployment is complete, open Internet Explorer and navigate to https://intranet.contoso.com/sites/Lab07/Lists/TimeSheets.
    3. Click the drop down menu on one of the documents in the list and select Workflows.
    4. Click the Timesheet Submission VS link to start the workflow.
    5. When the workflow is complete verify the Payment Ref was assigned and then click the Completed link.
    6. Verify the workflow history.

    Figure 4

    Workflow information

Note:
In this exercise you created a workflow using Visual Studio 2010.