HOWTO: Complete a WF Workflow if a Task Replicator Activity’s Tasks Haven’t Been Completed Yet

Scenario:

A Visual Studio Workflow Foundation Workflow needs to terminate with approval given the following circumstances:

1. At least one task has been completed out of the many tasks that were assigned via the workflow to approvers

2. A field on the current item the workflow is executing on has been edited/updated/changed

Thus the behavior would be that the users could modify the workflow by adding approvers, completing tasks, etc. and if the monitored field on the current list item hadn’t been updated, the workflow would remain open. As soon as the workflow received notification that the current item’s field had been updated and that it matched an internal comparison value, the workflow would evaluate if at least one task had been approved and conclude the workflow (including closing/completing all outstanding tasks).

Implementation:

Instead of using a Parallel Activity, a Condition Activity Group (CAG) was used. It included the 2 event handling scopes. The first contained the an activity that handled the update to the current item. The second contained a task replicator. The task replicator was responsible for creating the tasks as/per the add approver workflow modification data. The CAG was then given a declarative rule condition to monitor 2 Booleans (isAtleastOneTaskComplete and isItemUpdated) which are public Booleans set by each activity when a task completes or the item was updated. If those evaluate to true, the CAG terminates the task replicator and closes the workflow successfully.

Adding Approvers:

Once the CAG was implemented, the ability to modify the task replicator and add approvers was lost. This is because the replicator in the workflow (this.taskReplicator1) is not actually executing. The CAG creates a child instance of the task replicator and runs that one. Thus, when the first child is created, we capture that activities parent from its event args which resolves to the active (cloned) task replicator (not its template). When adding approvers, we add to that replicator’s children and not the template (non-executing one).

Clean Up:

However, while the CAG terminated the replicator, it still leaves the tasks. Using the MOSS API with a code activity, we got the task list from the workflow properties and found all tasks relating to the workflow instance running and closed them using the MOSS API. This allowed the tasks to be complete and the workflow to finish.