How to use api.QueryWorkList to Find Work Items based on ProcessInstanceID?

Objective

How to query a list of Work Items based on ProcessInstanceID using the api.QueryWorkList call?

Summary

There are number of ways to use QueryWorkList or QueryWorkListEx to pull out Work Items associated with different search criteria such as: Status, ProcessInstanceID, process model name, etc. You can construct a search criteria and then pass it in as an argument to the QueryWorkList or QueryWorkListEx call.

Resolution

The following is an example code of using QueryWorkListEx to retrieve the Work Items based on the process instance ID:

public WFManualWorkItem[] GetProcessInstanceWorkItems( 
      string ProcessInstanceID )
{
      string _searchClause = "";

try
     {
          if ( ProcessInstanceID.Length > 0 )
          _searchClause += "WF_MANUAL_WORKITEMS.PROC_INST_ID = '" +
          ProcessInstanceID + "'";

          WFManualWorkItem[] wks = api.QueryWorkListEx( _searchClause );
          return wks;
     }

catch ( Exception e )
    {
         throw new Exception( "Error with GetProcessInstanceWorkItems", e );
    }
}

When using the PROC_INST_ID in your search clause, make sure you specify the actual table name you want to search against as PROC_INST_ID exists in multiple tables.

The following is an example code of using QueryWorkList to retrieve the Work Items based on the Process Instance ID:

String piID = m_ProcInst.ProcInstID;
WFAny any = WFAny.Create(piID);
WFQueryExpr expr = new WFQueryExpr("PROC_INST_ID", SQLExpr.EQ, any, true);
WFManualWorkItem[] wks = api.QueryWorkList( expr );