Query all Sharepoint workflows for their internal status

sharepointworkflow

I'm trying to get the internal status of all sharepoint workflows.

Is there anyway to programmatically query for all workflows, and get their status? Either through a CAML query or the the object model?

I realize that I can iterate over all items in sharepoint, and see if there is a workflow attached, and then get the status. However, that is overkill, as there are 100,000s of items, and only a small subset have workflows. I want to run a periodic report to fetch the status of all workflows, without bringing the server to it's knees.

Thanks!
Dave

Best Answer

A view is, at its heart, a CAML query. The comment from @pst is a good choice if you don't want to iterate through all the items; just swipe the CAML from the view definition of any view that shows the workflow status column. The Lists.asmx Web service could come in handy here, though I usually use PowerShell on the server to extract view definitions.

If you do iterate through the items in the object model, here's a snippet to get rolling:

using (var site = new SPSite(url))
using (var web = site.OpenWeb())
{
    var manager = site.WorkflowManager;
    var list = web.Lists[listname];
    if (list == null || list.Hidden)
    {
        return "Cannot work under these conditions.";
    }

    foreach (SPListItem item in list.Items)
    {
        foreach (SPWorkflow workflow in item.Workflows)
        {
            if ((workflow.InternalState & SPWorkflowState.Faulting) == SPWorkflowState.Faulting)
            {
                Console.WriteLine(SPWorkflowState.Faulting.ToString());
            }

            // ...
        }
    }
}
Related Topic