Search This Blog

Saturday, March 19, 2011

Updating List Item from Workflow and avoid repeated firing of workflow

I have a list having a custom workflow attached to it. Workflow is executed whenever any updates are done to the item. Apart from performing various other task, the workflow also need to update a field of that item.

So what is the problem?

There is no issue updating any item from the workflow. You can use workflowProperties.Item["FieldName"] = "Value" to update any field. The side effect is updating the item triggers the workflow again as it is configured to execute when item is modified. This causes the workflow to run in indefinite loop.
So what is the solution?

Create a new class within the workflow and inherit it with SPItemEventReceiver. Create two new functions called DisableWorkflow() and EnableWorkflow().

Inside DisableWorkflow() add a line –
this.DisableEventFiring

Inside EnableWorkflow() add a line –
this.EnableEventFiring

That’s it, just create a object of this class, call DisableWorkflow() just before workflowProperties.Item.Update() .Then call EnableWorkflow() after updating the item.