3

I need to get the status of a Workflow programatically. Things i tried:

  • item[workflowName] - gave me a "8" altough the workflow is completed (seems to always return 8, I don't know why) (was not helpful)
  • SPFieldWorkflowStatus.GetFieldValueAsText(item[workflowName]) - returns an empty string
  • SPWorfklow.InternalState - was "locked | running | notStarted" in WorkflowStarting and WorkflowStarted Event. Was "completed" in WorkflowCompleted Event. Why was it in notStarted while in the WorkflowStarted Event?

Are there other ways to get the status of a workflow?

what i need

What i need to do for a customer is to create a column in a list where the customer is able to see the status of a workflow which runs on an other list (like a lookup, which i already tried to use but couldn't select the workflow field where the status is). That's why I'm now trying to solve it with a hyperlink field but I'm not able to get the correct status from a workflow.

3

Status workflow you can get using the following method

public string GetWorkflowStatus(SPListItem item, String workFlowName)
{
    SPWorkflowManager manager = item.Web.Site.WorkflowManager;
    foreach (SPWorkflow instance in manager.GetItemWorkflows(item))
    {
        if (instance.ParentAssociation.Name == workFlowName)
        {
            foreach (SPField field in instance.ParentList.Fields)
            {
                if (field is SPFieldWorkflowStatus)
                {
                    SPFieldWorkflowStatus statusField = (SPFieldWorkflowStatus)field;
                    if (statusField.Title == workFlowName)
                    {
                       int statusValue = int.Parse(item[statusField.StaticName].ToString());
                       return statusField.GetFieldValueAsHtml(statusValue);
                    }
                }
            }
        }
    }
    return String.Empty;
}
  • Returns an empty string - the same as statusField.GetFieldValueAsText(object) – Razze Mar 18 '14 at 14:13
2

The simplified version:

public static string GetWorkflowStatus(SPListItem listItem,  string workflowTitle)
{    
    var workflowStatusField = listItem.Fields[workflowTitle] as SPFieldWorkflowStatus;
    int statusValue = int.Parse(listItem[workflowStatusField.StaticName].ToString());
    return workflowStatusField.GetFieldValueAsHtml(statusValue);
}
0

It seems to be a problem with the amount of workflows running on that list. I read here that the status can return "8" if 15 or more workflows status fields exist in a list. We have currently 26 workflows running on that list and therefore I assume that's not possible to get the status without causing a ViewQueryOverflow.

  • Having so many workflows such a solution is not possible to work. You can try to change the approach and add to the list of hidden text column with the status of a workflow. During the run workflow at key moments to update this column. – Nidhogg Mar 18 '14 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.