Wednesday, April 11, 2007

Capturing onItemChanged event in Workflow

To catch the onItemChanged event of any Sharepoint list in a workflow, the following needs to be done:

  • Add a CallExternalMethod Activity, set the interface to Microsoft.SharePoint.Workflow.IListItemService
  • Set the method name to InitializeForEvents
  • Bind the ItemId, ListId to ItemID and ListId in the workflow properties.
  • Add a HandleExternalEvent activity and specify the following properties:
    InterfaceType to Microsoft.SharePoint.Workflow.IListItemService
    Event Name : OnItemChanged
    NOTE: The correlationToken should be the same as the correlation token specified for CallExternalMethod activity.
    Method Name. This method will be called whenever there is a change in the list item.
  • This will capture the onItemChanged event.

6 comments:

Anonymous said...

Hi, Amol Ajgaonkar!
Thank you for this post!
We have tried to use State Mashine Workflow in SharePoint 3.0 and your approach with InitializeForEvents was very helpfull.
But in our case it was crashed till we set ID property of CallExternalMethod activity to new GUID. Best regards from .NET Developers.

Amol Ajgaonkar said...

Thanks a lot for your feedback,
I shall include that in my post.

Anonymous said...

Once again greetings,

Can you help me with my problem.

I have StateMashine workflow that has 4 states. The first state handle WorkflowActivation event. Another two states capture OnItemChande events accordinaly to your post. Last state is Complited one. As you think why the event capturing doesn`t work in the second state?
The second state differs from the first by ID property and CorrealtionToken.

Amol Ajgaonkar said...

In the second state, the HandleExternalEvent Activity which checks the OnItemChanged event should have the same correlation token as that in the CallExternalMethod activity with the initializeForEvents event.
The correlation Token should be same for all OnItemChanged event handlers (i.e for the same List) and the InitializeForEvent CallExternalMethod Activity. Let me know if you still have doubts

Anonymous said...

Thanks for your help.
My mistake was in that that I used ExternalMethod: "InitializeForEvent" twice in both states. In second state this call hanged Workflow.

Anonymous said...

Hi Amol,
Really useful post, thank you.
One question, what is the logic behind having to set up the "CallExternalMethod" what does that part do? I would have thought all you would need was the HandleExternalEvents part (I realise it doesn't work if you just do that, I just don't understand why).