Tuesday, 19 May 2020

cancel the workflow and cycle throught its tasks.

Here is my code block to cancel the workflow and cycle throught its tasks.

SPWorkflowCollection itemWorkflowCollection= listItem.Workflows;

foreach (SPWorkflow itemWorkflow in itemWorkflowCollection)

{

//cycle workflows associated to the item (listItem)

 

if (!itemWorkflow.IsCompleted && itemWorkflow.InternalState == SPWorkflowState.Running)

{

foreach (SPWorkflowTask taskWorkflow in itemWorkflow.Tasks)

{

//cycle throught all tasks associated to the workflow

 

//if task is not completed

if (taskWorkflow["PercentComplete"].ToString() != "1")

{

 

//you can cancel or change the running tasks before canceling the workflow

taskWorkflow["Status"] = "Canceled";

taskWorkflow["PercentComplete"] = 1;

web.AllowUnsafeUpdates = true;

taskWorkflow.Update();

}

}

SPWorkflowManager.CancelWorkflow(itemWorkflow);

}

}

No comments:

Post a Comment