2

I need to be able to get the status of a SP 2013 workflow using C#.

I've tried getting the WorkflowManager for the SPSite and calling the GetItemWorkflows, but I never get any results from that. The documentation suggests that I'll get the workflow instances that have ever run on a specific SPListItem.
So far I've not seen this work at all. Does anyone have any ideas around this? I must be missing something, because I can't imagine what I'm looking for is that difficult.

Btw, I'm running SharePoint Server Enterprise 2013 on-prem.

1
  • as I've just started messing with the 2013 workflows in 2013 and SharePoint online... I've just hit this problem. Is there an answer or method to get this information for non-programmers? – Carp Apr 19 '17 at 13:26
2

Unfortunately, contrary to the post from MAllen, finding out if the SPWorkflowState is Running, Cancelled, Completed, Expired, Faulting, Locked, Suspended, Terminated, etc. is not the same thing as finding out the actual status of an item's workflow, as displayed in SharePoint on a list item.

There is usually a column displayed for a workflow. Let's say the workflow is called "Messages". There would be a "Messages" column on the list it is associated with. (The workflow could also be associated with a content type, but the content type still has to be associated with a list, so the column should still be on that list. From SP 2010 on, if you have the max number of lookup fields on your view already, I believe 8, the column will not get created when the workflow is added. You'd have to lower the number of lookup fields in the view and remove/re-add the workflow.) Using

myListItem["Messages"].ToString();

in this example, should give a number corresponding to the actual status. Here is the key for figuring it out:

0 = Starting
1 = Failed on Start
2 = In Progress
3 = Error Occurred
4 = Cancelled
5 = Completed
6 = Failed on Start(Retrying)
7 = Error Occurred (Retrying)
15 = Cancelled
16 = Approved
17 = Rejected
null = Non-Start
0

You should be able to use Microsoft.SharePoint.Workflow.SPWorkflow.InternalState to get the Microsoft.SharePoint.Workflow.SPWorkflowState which will provide you with the status. This is normally accessed by using workflowProperties.Workflow.InternalState in your C# code.

I hope that helps.

SPWorkflowCollection itemCollection = mylistItem.Workflows;
foreach (SPWorkflow itemWorkflow in itemCollection)
{
    if (itemWorkflow.InternalState == SPWorkflowState.Running)
    {
        //...
    }
}

Possible states you get returned:

None

Locked

Running

Completed

Cancelled

Expiring

Expired

Faulting

Terminated

Suspended

Orphaned

HasNewEvents

NotStarted

All

EDIT: This is a much more reliable way of knowing what the workflow state is (coming from the workflow service manager) instead of relying on data that is input into a column on the list item. You can make logical choices such as if Suspended attempt to restart the workflow instance programmatically.

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.