How to check the status of a process instance after synchronously actioning a worklist item
It is often desirable to enforce in your application that upon user submission of a task (e.g. the user clicks the submit button after making a decision that finishes he/her workflow client event), that the application does not proceed until the K2 process has finished handling this submission. If some sort of error was encountered within the process execution, K2 will not automatically bubble it up to the UI layer. However, many application designers would like to know if there was some sort of error that occurred within K2 processing and inform the user.
First of all, to make sure that the K2 API (SourceCode.Workflow.Client) properly blocks return to the calling application, the Action must executed with the synchronous flag set to true. For example:
oWli.Actions[0].Execute(true);
This means that execution focus will not return back to the calling application until the K2 process execution has hit a stopping point. A stopping point could be either a Client Event, the end of the process instance, or an error within the process. However even though K2 appropriately blocks the return execution, as of this writing (blackpearl SP1), the status of the process instance (WorklistItem.ProcessInstance.Status1) is not automatically updated on the object from the client application. Thus if you check the status of the process instance on your existing WorklistItem.ProcessInstance object, it will be the same as before the call, no matter what happened in the process execution.
There is an easy way to query the process instance status by using the SourceCode.Workflow.Client.Connection.OpenProcessInstance method for the given process instance ID. Below is a simple code sample that retrieves a worklist item, actions it synchronously then checks the proc inst status via the OpenProcessInstance method:
// open the K2 connection
SourceCode.Workflow.Client.Connection oConn = new SourceCode.Workflow.Client.Connection();
oConn.Open(m_strK2Server);
// action open the worklist item
SourceCode.Workflow.Client.WorklistCriteria oCrit = new SourceCode.Workflow.Client.WorklistCriteria();
oCrit.AddFilterField(SourceCode.Workflow.Client.WCField.ProcessFolio, SourceCode.Workflow.Client.WCCompare.Equal, m_strFolio);
SourceCode.Workflow.Client.Worklist oWl = oConn.OpenWorklist(oCrit);
SourceCode.Workflow.Client.WorklistItem oWli = oWl[0];
// get the proc inst id for later use
int nProcInstID = oWli.ProcessInstance.ID;
// complete the action synchronously, be sure to set the sync flag = true
oWli.Actions[0].Execute(true);
// open the proc inst for this id.
SourceCode.Workflow.Client.ProcessInstance oProcInst = oConn.OpenProcessInstance(nProcInstID);
if (oProcInst.Status1 == SourceCode.Workflow.Client.ProcessInstance.Status.Error)
{
MessageBox.Show("This process is in an error state.");
}
// close the K2 connection
oConn.Close();