Tuesday 17 July 2012

How to Check Workflow Status programmatically

How to Check Workflow Status programmatically
Today I'll show checking all workflow statuses in SharePoint site programmatically. Sometimes you may want to check workflow statuses are error or not.







You need workflow status name (list column) when you get a item's workflow status. But you can't get workflow status name from SPWorkflow properties directly. You need to use SPWorkflow.AssociationId property and SPList.WorkflowAssociations property to get workflow status.


using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;

// This is Console Application //

SPSite site = new SPSite("http://sharepoint-site");
SPWeb web = site.OpenWeb();

foreach (SPList list in web.Lists)
{
foreach (SPListItem item in list.Items)
{
foreach (SPWorkflow workflow in item.Workflows)
{
try
{
Console.WriteLine("item url : " + item.Url);
Console.WriteLine("workflow name : " + list.WorkflowAssociations[workflow.AssociationId].Name);
Console.WriteLine("workflow status : " + item[list.WorkflowAssociations[workflow.AssociationId].Name]);
Console.WriteLine();
}
catch (ArgumentException)
{
//ArgumentException is throwed if the workflow is not exist in the list column.
Console.WriteLine("Workflow name : {0} is already not exist in List title : {1}.",
list.WorkflowAssociations[workflow.AssociationId].Name, list.Title);
}
}
}
}

No comments:

Post a Comment