public static SPListItem WorkflowTaskItemBasedOnListItem(SPWeb web, string listName, string workflowTaskListName, int itemId)
{
SPListItem workflowItem = null;
SPList workflowTasks = web.Lists[workflowTaskListName];
SPList mainList = web.Lists[listName];
SPListItem listItem = mainList.GetItemById(Convert.ToInt32(itemId));
SPWorkflowFilter filter = new SPWorkflowFilter();
filter.InclusiveFilterStates = SPWorkflowState.Running;
// Get a list of workflow tasks associated with current Item
SPWorkflowTaskCollection workTaskColl = web.Site.WorkflowManager.GetItemTasks(listItem, filter);
foreach (SPWorkflowTask task in workTaskColl)
{
if (task != null)
{
workflowItem = workflowTasks.GetItemById(task.ID);
break;
}
}
return workflowItem;
}
This function returns a workflow item based on Parent List item Id. It reads the List Item and finds the associated workflow which are still running against this list item.