Monday, August 29, 2011

SharePoint 2010 Workflow using Visual Studio 2010

In My previous posts I have taken a developer approach to prescribe the things to go for while developing some custom SharePoint solutions, but here Iwill show one by one every step to achieve this. You’re about to witness an evolution in building workflows for Microsoft, no need for command line deployment, specific configuration of workflow features, and dealing with workflow and feature xml files.


What I’m about to show you is what I did and tested in the following environment:
• Microsoft Windows server 2008 R2 Enterprise Edition

• SharePoint Server 2010 Enterprise Edition

• MS SQL 2008 Enterprise Edition

• Visual Studio 2010 Ultimate

• Code was done in C# only

(Note: we don’t need the exact environment, but as such a similar one to get started)
1. In your SharePoint 2010 create a Document Library (unless you already have one) which can be easily achieved by choosing “New Document Library” from the “Site Actions” menu (located in the top left corner) ,give it a name “Here I have Given: Tax Reports” and click “Create”.
Now you need to create a mock contract template in Word 2010 for your Document Library

2. When in your Document Library, click the Library Settings in The top Ribbon under Library Tag.

3. On the Customize Documents page, locate the Columns section.

4. Create a Employee custom column:


o Click Create Column.


o Type in the column name: “Employee”.


o Select the type Single line of text.


o For the “Require that this column contains information setting” click Yes.


o Click Ok to add the new column.

 5. Add two more columns: a “Employer” column that is type Single line of text and a “TaxAmount” column that is type Currency. Both columns should be required.



6. Return to the document library home page.

7. Under Library Tools, extreme left select “New Document” and create a document.

8.       A new document appears in Word 2010,choose “Enable Editing”. Go to File and observe that there are text boxes for the custom columns from the document library:


9. On the Office menu click Save As. Save the document as %PATH OF YOUR CHOICE%\Contract.docx. If you are prompted to confirm saving to the new Word 2010 file format, click “Ok”.


10. Insert content controls for each of the server properties:
o Place the selection in the document where you want the content control to be placed.
o Select the Insert tab on the ribbon.
o Click Quick Parts, then click Document Property, and then click “Emplyee”.
o Repeat these steps to add content controls in the document for the Employer and the Tax Amount properties.
o For now, leave the content controls empty – do not add text to them.

11. On the Office menu, click Exit Word. When prompted to save changes to the document, click “Yes”.



12. Return to the document library in Internet Explorer, click Settings and then select Document Library Settings.


13. On the “Customize Documents” page, locate the “Content Types” section and click the Document link.

14. NB. If you can’t locate the “Content Types” section, in the “General Settings” section locate “Advanced Settings” in there switch the radio button under “Allow management of content types” to “Yes”, now when you come back to the “Customize Documents” page you should be able to locate the “Content Types” section.


15. On the “List Content Type: Document page”, click the “Advanced Settings” link.


 16. Select the Upload a new document template radio button and then click Browse. Browse to select %PATH OF YOUR CHOICE%\Contract.docx and then click Ok.

17. In Internet Explorer, return to the document library.


18. Upload a new document:

o In the document library, click the “New Document” Library Tools and then click “Document”. Word starts and loads a new document based on the contract template you created.
o Fill in the values for Title, Employee, Employer and TaxAmount. You can fill in the values on the document properties bar (just below the ribbon) or you can fill in the content controls: the end result will be the same.
o Once all required fields have been filled in, on the Office menu, click Save.
o Save the document with a filename of your choice to the document library. If you receive a message that the document must be checked in for it to be visible to others, click OK.
o On the Office menu, click Close. When prompted to check in the document, click “Yes”.
o In the Check In dialog, click Ok to accept the version defaults. Exit Word.
o Your new document appears in the document library. Notice that the Employee, Employer and TaxAmount columns reflect the values you entered in the Word document.


19. You may upload additional documents if you wish.


20. Close Internet Explorer.

Now comes the fun part, creating the workflow itself


21. Start Visual Studio.

22. On the File menu, click New and then click Project. The New Project dialog appears.

23. In the list of Project Types, expand SharePoint select 2010.

24. Select the Sequential Workflow project type.

25. Name the project TaxWorkflow, specify the default location and click “OK”.

 
26.   The New SharePoint Workflow wizard appears. Change the workflow name to TaxWorkflow, use your local site where you created your Document Library (eg http://localhost/yoursite) for debugging and click “Next”.
27. Select ListWorkflow and press next, and then choose the default debug setting and library to associate workflow with

Note: If History, Task List is not presented it would not let you create the workflow, please create Task lIst in case of a blank Site template.


29. The new workflow appears in the designer.


Add a CreateTask Activity to the Workflow


30. If the toolbox is not displayed, then on the View menu, click Toolbox.

31. From the SharePoint Workflow tab on the control toolbox, drag a CreateTask activity and drop it onto the workflow designer between onWorkflowActivated1 and the end of the workflow ( ). The new activity is named createTask1 by default.






32. Configure the TaskID property of createTask1:

o In the Properties window, click the ellipsis for the value of the TaskID property. The Bind ‘TaskID’ to an activity’s property dialog appears.
o Type the new member name myTaskId, ensure that the Create property radio <>button is selected and click OK.




33. Configure the TaskProperties property of createTask1:

o In the Properties window, click the ellipsis for the value of the TaskProperties property. The Bind “TaskProperties” to an activity’s property dialog appears.
o Type the new member name “myTaskProperties”, ensure that the Create property radio button is selected and click OK.

34. In the properties window, select the MethodInvoking handler. Type MyTaskCreation and press the Enter key. The code window appears.

35. Add code to the MyTaskCreation event handler and also add the CustomFieldText function.















36. On the View menu, click Designer to return to the workflow designer.


37. In the Properties window, select the text box for the CorrelationToken property value. Type myTaskToken and press the Enter key.

38. Expand the CorrelationToken property to display the OwnerActivityName subproperty.

39. For the OwnerActivityName subproperty, select Workflow1 in the dropdown.

Add a While Activity to the Workflow

40. If the toolbox is not showing, then on View menu, click Toolbox.

41. Drag a While activity and drop it between createTask1 and the end of the workflow ( ). The new activity is named whileActivity1 by default.

42. In the Properties window, locate the Condition property for whileActivity1.




















43.   Select Code Condition in the property value

44. Expand the Condition property to display the Condition subproperty.

• 1. For the Condition subproperty, type myTaskNotCompleted and press the Enter key. The myTaskNotCompleted event handler appears in the code window.
• 2. In the Workflow1 class, add the taskCompleted variable at the class-level.







 • 3. Add code to the myTaskNotCompleted event handler that will return a result indicating whether or not the task has completed; the task is completed when its PercentComplete property is 1.0.





Add an OnTaskChanged Activity to the Workflow

48. On the View menu, click Designer.

49. If the toolbox is not visible, then on the View menu, click Toolbox.

50. From the SharePoint Workflow tab on the toolbox, drag an OnTaskChanged activity and drop it in the center of whileActivity1. The new activity is named onTaskChanged1 by default.

















 
51. In the Properties window, set the CorrelationToken property of onTaskChanged1 to myTaskToken.


52. Set the AfterProperties property of onTaskChanged1:

o In the Properties window, select the ellipsis for the AfterProperties property.
o On the Bind ‘AfterProperties’ to an activity’s property dialog, select the Bind to new member tab.
o Name the new member afterMyTaskPropertyChange, ensure that the Create Property radio button is selected and click OK.

53. Set the TaskId property of onTaskChanged1:

o In the Properties window, select the ellipsis for the TaskId property.
o On the Bind to an existing member tab, select the myTaskId property and click OK.

54. Double-click onTaskChanged1 in the workflow designer. The Invoked event handler for onTaskChanged1 appears in the code window.

55. Add code to onTaskChanged1_Invoked that will set the taskCompleted variable to true when the PercentComplete property of the task is 1.0.




56. In the Workflow1 class, set breakpoints on the MyTaskCreation and onTaskChanged1_Invoked methods.

Note: To set the breakpoints, select the first line of each method in the code windowand press the F9 key.

57. Press F5 to start debugging the project.

Note: Watch the output window as the project is built and deployed. The output window shows you many of the steps that VSTO takes for deploying the workflow solution - without VSTO, these are all steps that you would take manually. One of the SharePoint requirements for a new workflow solution is that the SharePoint worker process and IIS must be restarted; thus build/deploy times may vary.


58. Internet Explorer starts and displays the document library with the document(s) that you uploaded.

Note: You might encounter the following warning (below), if you do, click “No” do as it said and start again.



59.   Next Just upload a new document and will able to see the current progress:
60.   Then go Tasks List.


61.You can see a new task created above.


Go Ahead and Edit the document and you can see the status as above.

Just make it 100% complete.




And you can see that TaxWorkflow Completed...