The World of Workflow Analysis and Reporting :)

A piece of code to see workflows and its instances

int WorkflowCount = 0;
Console.WriteLine("Start:" + DateTime.Now.TimeOfDay.ToString());
using (var site = new SPSite(http://sitecollection)) // Change here with site collection url
{
using (var web = site.AllWebs["/web/"]) // change here with web site url of use RootWeb
{
var manager = site.WorkflowManager;
foreach (SPList list in web.Lists)
{
if (!list.Hidden || list.BaseTemplate == SPListTemplateType.XMLForm || list.BaseTemplate == SPListTemplateType.DocumentLibrary)
{
foreach (SPListItem item in list.Items)
{
foreach (SPWorkflow workflow in item.Workflows)
{
if ((workflow.InternalState & SPWorkflowState.Running) != SPWorkflowState.Running)
{
WorkflowCount += 1;
} } } } } } }
Console.WriteLine(WorkflowCount);
Console.WriteLine("Start:" + DateTime.Now.TimeOfDay.ToString());
Console.ReadLine();
Ouput

Start:12:06:53.9282671
1159
End:12:07:58.4902734

 
It took me over 1 minute to find the number of instances.  Not good if you want to show it to a Workflow Admin.  Another reason for slowness is my current development machine (P4 with 3 GB RAM is not a good one).  You also have other properties available using Workflow.Parent which is a list item.  One you have it then you can do whatever you want.
Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s