November Happy Hour will be moved to Thursday December 5th.

Is it possible to get running workflow instances in code?

Vote:
 

We currently have an issue with the My Tasks list in the CMS. The AJAX service it goes to returns a 404 when a page referenced by a running workflow instance no longer exists (delete page and clear recycle bin). This means the entire My Tasks list fails to work.

As a workaround, we would like to be able to get a list of all currently running workflow instances of type "Parallel Approval" in code, so that we can delete those which have invalid content references associated with them.

Is it possible to -

  • List all running workflow instances in code
  • Delete a running workflow instance in code

Thanks.

#90525
Sep 11, 2014 11:13
Vote:
 

Hi Jallen,

1. List all running workflow instances in code:

WorkflowSystem.InstanceHandler.GetInstances(AccessLevel.Read);



2. Delete a running workflow instance in code:

WorkflowSystem.InstanceHandler.TerminateInstance(instance.InstanceId, "Terminate instance from code");

Hope that help!

//Ha

#90550
Sep 12, 2014 6:48
Vote:
 

That's perfect, thanks!

#90559
Sep 12, 2014 9:43
Vote:
 

If anyone has a similar problem to what I described in OP, this is the code I have in my scheduled job now to fix the problem. Hopefully someone else will find it useful -

var instances = WorkflowSystem.InstanceHandler.GetInstances(AccessLevel.NoAccess);
_numTotalInstances = instances.Count;
_numDeletedInstances = 0;
foreach (var instance in instances)
{
    if (_stop)
    {
        return;
    }

    try
    {
        _contentRepository.Get(instance.PageLink);
    }
    catch (ContentNotFoundException ex)
    {
        _pageReferences.Add(instance.PageLink.ToString());
        WorkflowSystem.InstanceHandler.TerminateInstance(instance.InstanceId, "Invalid page reference", AccessLevel.NoAccess);
        _numDeletedInstances++;
    }
}
#90568
Sep 12, 2014 10:39
Vote:
 

Nice code Jallen! Thanks!

// Ha

#90571
Sep 12, 2014 12:10
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.