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.
- Open Visual Studio 2010 and create a new workflow project:
- Select the Sequential Workflow template in the Visual C#/Visual Basic » SharePoint » 2010 templates folder and give it the name VSSubmissionWorkflow.
- 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.
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
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
- Click the Finish button.
- Add a new Code Activity from the Windows Workflow v3.0 section of the toolbox.
- Add it immediately following the onWorkflowActivated1 activity.
- Set the name of the new code activity to SubmitToHR using the Properties window.
- Double-click the activity to generate the ExecuteCode event handler.
- 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.
private void SubmitToHR_ExecuteCode(object sender, EventArgs e) { SPListItem item = workflowProperties.Item; item["Payment Ref"] = Guid.NewGuid().ToString(); item.Update(); }
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
- At this time you are going to prepare and deploy the workflow
- Setup the auto association parameters that will tell Visual Studio to automatically associate your workflow with the Time Sheets list when it is deployed.
- Select Workflow1 in the solution explorer.
- In the Properties window, set the Auto Associate property to True.
- In the History List property click the [...] button to start the wizard.
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
- On the final page check the first check box and clear the rest and click Finish.
Deploy the workflow to SharePoint and verify that it works.
- Right click the project in solution explorer and click Deploy.
- When the deployment is complete, open Internet Explorer and navigate to https://intranet.contoso.com/sites/Lab07/Lists/TimeSheets.
- Click the drop down menu on one of the documents in the list and select Workflows.
- Click the Timesheet Submission VS link to start the workflow.
- When the workflow is complete verify the Payment Ref was assigned and then click the Completed link.
- Verify the workflow history.
Figure 4
Workflow information
In this exercise you created a workflow using Visual Studio 2010.