I had a problem with creating and using tasks in a SharePoint state machine workflow so I wanted to capture it in a blog so that others wouldn't need to. There isn't a lot of magic just a few gotchas which I'll point out.
Let me start by showing an overview of the workflow we are trying to create.

This is about as simple as it gets. Start -> Manager Approve -> Done.
If you look at "ManagerReview" you see we have three things.
- ManagerReviewInit - This activity is responsible for creating the task
Gotcha #1 - If we create the task here we encapsulate the ability that later in the workflow we can transition back to the ManagerReview state and it will know how to create a new task. - OnManagerReviewTaskCreatedEvent - This activity is going to initialize the task
- OnManagerReviewTaskChangedEvent - This activity is going to perform the logic of when the task is completed
ManagerReviewInit

No surprises here. Unless you take a look at the CorrelationToken on the create task.

Gotcha #2 -
The OwnerActivityName needs to be scoped at the "State" level
(in our case "ManagerReview"). This is important because later if
you add a state that transitions back to "ManagerReview" you will
get an exception stating that the correlation token was already
initialized. Narrowing the scope will invalidate the correlation token
when you leave the state.
OnManagerReviewTaskCreated

Gotcha #3 -
At first I assumed that you needed to have a "set state" at the end
of every event to loop back onto itself. Well you don't, there is an implied loop back. In fact if you do, the state initialization
routine will be called causing a task to be created which will
basically create an infinite recursion.
OnManagerReviewTaskChanged

If we take a closer look at the conditional you'll see I'm using a property I created, TaskComplete.

Gotcha #4 -
Microsoft doesn't expose the task status and since
most users don't update percent complete when completing tasks you have to attach to the changed event
and expose it to the workflow.
public bool TaskComplete { get; set; }
private void OnManagerReviewTaskChanged_Invoked(object sender, ExternalDataEventArgs e) {
TaskComplete = AfterTaskProperties.ExtendedProperties[SPBuiltInFieldId.TaskStatus].ToString() == "Completed";
}
1 Comment
Leave a comment
0 TrackBacks
Listed below are links to blogs that reference this entry: Creating and Using Tasks in SharePoint State Machine Workflow.
TrackBack URL for this entry: http://www.nearinfinity.com/mt/mt-tb.cgi/624



I really liked your method. thanks for sharing this:) hope many people will find it useful as I did. have read lots of articles on the topic, but have never thought that could be so easy