I was sitting in the San Jose convention center after Office Dev Con was all over, killing time before my flight home, chatting with Andrew when he asked me a workflow question. Not only did I not know the answer, it reminded me that I was sorely behind on figuring it out and posting it on my blog. I had first looked into this way back in July. I figured it was time to crack this nut once and for all.
So here it is: http://www.mannsoftware.com/Files1/DynamicTasksWF.zip. It contains a sample Visual Studio 2005 project that demonstrates:
1. Creating a dynamically specified number of Tasks using a Replicator. Right now the number is simply specified by a variable. It would be very easy to retrieve the number from a Workflow Initiation form
2. Marking the Workflow “Complete” when the majority of those Tasks have been marked “Complete”
A few notes:
1. Everything is inside a ConditionedActivityGroup because I had visions of doing some logging or other processing before/after the Replicator (one thing that comes to mind is marking the “incomplete” Tasks as a Status of “Deferred”)
2. The only tricky part is that the Replicator contains a Sequence and the Sequence contains the CreateTask. The “Owner” of the CorrelationToken for the CreateTask is set to the Sequence rather than the Workflow itself. This allows the Replicator to re-gen a CorrelationToken for each Task and keep them all straight
3. The Replicator is set to run in Parallel so all of the Tasks are created at the same time
The rest is pretty straightforward. I even moved all of my until/when and while conditions into Code so that they’re easier to see when you’re looking at the .cs file.
As always, feel free to make use of the code. If you have questions, you can contact me and I’ll do my best to answer. This is just a proof-of-concept that I tossed together pretty quickly so please don’t use it in a Production environment without a lot of testing. You’ll need the VS Extensions for WF installed on your machine, and I did this project in VS 2005.