Wednesday, June 17, 2015

Get workflow status programmatically in SharePoint

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;
}

No comments:

Post a Comment