One of my customer would like to call “onWorkflowItemChanged” multiple times until the he got a specific value in a list item column. I have prepared the following solution to resolve the problem :-

 clip_image001

  • Created a State Machine workflow
  • Added two new states named “DocumentProcessed” and “StopWorkflow”.
  • Set the “StopWorkflow” activity’s state as “Completed State”.
  • Add an eventDrivenActivity in the “DocumentProcessed” state. Inside the eventDrivenActivity add the following activities :-

    clip_image002
  • In the IfElseBranchActivity1, set the condition property to code condition and call the check status method (which reads the column from list item and set the result.)

Code Sample

===========

   1: private void checkStatus(object sender, ConditionalEventArgs e)
   2: {
   3:             SPListItem item = workflowProperties.Item;
   4:             if (item["Status"].ToString().ToUpper() == "PENDING APPROVAL")
   5:             {
   6:                 e.Result = true;
   7:             }
   8:             else
   9:             {
  10:                 e.Result = false;
  11:             }
  12: }
  • Set the setstateActivity2 ”DocumentProcessed” and setstateActivity4 ”StopWorkflow”.
  • Compile the code and deploy the solution. Add a column name “Status” in Custom List.
  • To test the solution, add the item and set the status column value “Start”. Workflow will start with “InProgress” state.
  • Edit the item and change the status to “Pending approval”. Till then the status is pending approval you can modify the List Item and it will fire the “OnWorkflowItemChanged”.
  • Once you changed the status column value to ‘Complete”, it will complete the workflow and set the workflow status to “Completed”.