Get workflow status programmatically in SharePoint

Hi,

I am writing very simple method which will return the workflow status in form of  string.

protected string GetWorkflowStatus(SPListItem itm)

{

string workflowName = “ApprovalStatus”; //name of the workflow

string statusText = string.Empty;

try

{

SPWorkflowManager manager = itm.Web.Site.WorkflowManager;

//SPWorkflowFilter filter = new SPWorkflowFilter()

//{

//    ExclusiveFilterStates = SPWorkflowState.Completed | SPWorkflowState.Cancelled

//};

//Get a list of the workflows that are running

foreach (SPWorkflow instance in manager.GetItemWorkflows(itm))

{

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(itm[statusField.StaticName].ToString());

statusText = statusField.GetFieldValueAsHtml(statusValue);

break;

}

}

}

 

}

 

}

}

catch (Exception ex)

{

log.LogInfo(ex.Message, ex.StackTrace);

}

return statusText;

}

Advertisements

5 thoughts on “Get workflow status programmatically in SharePoint

  1. You should use

    SPWorkflowCollection workflows = manager.GetItemWorkflows(itm)
    foreach (SPWorkflow instance in workflows)
    {
    }

    instead of

    foreach (SPWorkflow instance in manager.GetItemWorkflows(itm))
    {
    }.

    It is much more efficient.

    • I know this is an old post but why is it more efficient? manager.GetItemWorkflows(itm) is only executed once in both scenarios isn’t it? I’d say they’re pretty much equal and I’d probably choose the shorthand one myself as you save one line of code and it is just as readable.

  2. manager.GetItemWorkflows(itm) always return null however there is a workflow already running on item ,could you please help me

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s