Home > Deveploment and Programming, SharePoint Development > Get workflow status programmatically in SharePoint

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
  1. Jarkko
    21/10/2011 at 3:53 pm

    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.

    • 26/10/2011 at 12:35 pm

      Its a good practice.

    • tom
      27/01/2012 at 7:29 am

      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. 24/07/2015 at 12:13 pm

    Reblogged this on Prasad's Blog.

  3. Alshymaa
    24/08/2015 at 7:51 pm

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

  1. No trackbacks yet.

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 )

w

Connecting to %s

%d bloggers like this: