Step by Step Tutorial. Creating Workflows for Windows Sharepoint Services and MOSS2007 (part 12/20). Managing several approvers/reviewers
By Serge Luca
MVP Connected System
The Out-of the box approval workflow provided with MOSS allows to specify several approvers. In this post, I will illustrate how to do this. In a first time (part 1) , I will show you how to use the Replicator activity that you will need, and in a second time (part 2), you will apply this knowledge to our Sharepoint problem. If you know how the Replicator activity works you can directly go to part 2.
Part 1. Using the Replicator activity
The Replicator activity is part of the Workflow Foundation Base Activity Library, is somewhat similar to the While activity.
- Create a Sequential Workflow console application project.
- Add a Replicator activity into the workflow surface
- Add a custom Activity to the project; call it "ManageTask":
- Define a public string member called Approver in the custom activity :
- Drag and drop a Code activity in the custom activity
- Display the Approver member in its Event handler:
Recompile the project and drag & drop the custom activity into the Replicator:
- In the workflow code add an ArrayList data member and name it Approvers :
- In the Workflow constructor, add 2 users to the ArrayList
- In the Replicator activity, databind the InitialChildData property to the ArrayList:
In the Replicator activity, double click on the ChildInitialized event to generate an event handler :
- Each item in the collection will be associated with a child activity (the child activity here is ManageTask): each time a child activity will be created, the ChildInitialized event will be triggered, which give us the possibility to pass the associated data :
Run the application:
You can download the code here.
Part 2. Using the Replicator activity in the Approval workflow
- as I did in the previous post, I will start by using the HelloWorldSequential sample provided with the MOSS sdk. Don’t forget to replace the string http://locahost in the install.bat file with you own web site/site collection url. (See step 11 of this tutorial for more infos about this sample).
- The strategy is to replicate the following selected activities in a Replicator activity :
We will group these activities in a custom sequential activity.
- Add a new project (Sharepoint-Sequential Workflow Library ) to the current solution
- Remove the following files from the project :
- In the same project, add a new custom activity, name it ManageApproval:
- You can either drag & drop the activities from Workflow1 to the new custom activity or adding new activities in the ManageApproval activity (my option) :
- Link each of these activity to the same correlation token : TaskToken and ManagerApproval for the OwnerActivityName
- Select the CreateTask activity and databind its TaskId and TaskProperties property to new fields :
TaskId and TaskProperties
- Databind the TaskId prperty of the OnTaskChanged and CompleteTask activities to the newly created taskId member.
On the OnTaskChanged activity, databind the AfterProperties and BeforeProperties to new fields:
AfterProperties and BeforeProperties
- Define the following members (make sure Assignee is public since it will get the task owner from the Replicator activity) :
- Double click on the CreateTask activity to generate a handler and add the following code:
(most of the code can be copied from Workflow1.cs)
- Set the Condition property of the WhileActivity to Code Condition and name the function TaskNotFinished :
- Define a bool data member, isFinished , and check its status in the TaskNotFinished function:
- Double click on the TaskChanged activity to generate a handler and add the following code:
- Sign you assembly
- Get back to the workflow project and double click on the workflow code to display it in the designer; clean it up by deleting the activities you don’t need anymore; you must have something like this:
- Recompile the solution to make sure your custom activity will show up in the toolbox.
- Add a Replicator activity just after the OnWorkflowActivated activity and drag & drop you custom activity in it.
- In the workflow code, define a new member to host the assignees:
- Store the assignees into this ArrayList: click on the OnWorkflowActivated activity and add the following code in the handler:
- Bind the Replicator activity to the Assignee member.
- Add a handler to the ChildInitialized event of the Replicator and insert the following code:
- Modify the install.bat file in order to register the custom activity in the Gac :
"%programfiles%\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe" -uf MyApprovalActivities
"%programfiles%\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe" -if bin\Debug\MyApprovalActivities.dll
- Test the workflow: rebuild, install the workflow, add the association and start a workflow ; assign to user separated with a ";" , for instance U2UCOURSE\Administrator;U2UCOURSE\serge, like this :
A first task will be created for the user "Administrator":
- If the Administrator clicks on its task :
- Then, if the user "Administrator" select the "I am finished reviewing" and click the Submit button, a new task will be created for the user "serge":
- The reason Serge’s tasks is created after the Administrator’s task completes is that the ExecutionType property of the Replicator activity was set to Sequence. Let’s change it to parallel, rebuild and test the workflow .
This hands-on training is the property of Redwood S.L sprl and may not be organized in class or in group without the prior written permission of Serge Luca. Should you wish to organize this hands-on training in your company or institution, please contact Serge Luca first to enter into a licence agreement. Each trainer or teacher using this hands-on training should have a licence agreement. Please ask your trainer or Serge Luca whether he or she has entered into a licence agreement with Redwood S.L sprl.
The hyperlink to this hands-on training may be placed on your website for free, on the condition that the name Serge Luca is clearly mentioned in the reference. Please send us a mail containing the link to the web page our reference is used on.