Development

MS CRM 2011: How to cancel/postpone Workfow Instance using C#

Today I had a task to cancel about 25k instances of the same workflow. Of course it was possible to create Advanced Find View and go through 100 pages cancelling all workflows on the page. But I’m too lazy and I know C# to do that manually.

First code that I wrote used SetState message to Cancel change state of workflow instance but during first lunch I’ve got an exception that SetState message is not applicable for AsyncOperation entity. I went to SDK and found following explanation:
Updates an asynchronous operation (system job). Typically, you would update only the AsyncOperation.StateCode or AsyncOperation.PostponeUntil attributes of the asynchronous operation (system job). You can also call the IOrganizationService.Update method.
So I rewrote my code using Update message:

UPD: Here is the code that allows to postpone asyncoperation till some date:

2 Comments on “MS CRM 2011: How to cancel/postpone Workfow Instance using C#

    1. Yannick,
      Thanks for reference but what if I need to cancel workflow inside plugin or custom workflow activity? I believe that I can use code that I published but solution you sent a reference will be unusable.
      Andrii

Leave a Reply

Your email address will not be published. Required fields are marked *