Workflows Inventory Report for SharePoint
Requirement from my CIO: Salaudeen, Get me a SharePoint Workflows Report with Total No. of workflows wherever its running, for our intranet SharePoint site with Site Name, List Name, Workflow Name, and No. of Instances, etc.
Solution: As usual dive into the object model code (This can be achieved with PowerShell as well). Here it is.
This time the requirement is: Get me the workflows for all the lists and Libraries with No. of workflows completed, In Progress, Terminated, Cancelled.
Alright, I changed the code slightly:
Solution: As usual dive into the object model code (This can be achieved with PowerShell as well). Here it is.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; using Microsoft.SharePoint.Workflow; using System.IO; namespace GetWorkflowReport { class GetWorkflowsReport { static void Main(string[] args) { string site; try { if (args.Length == 0) { Console.WriteLine("Enter the Web Application URL:"); site = Console.ReadLine(); } else { site = args[0]; } SPSite tmpRoot = new SPSite(site); SPSiteCollection tmpRootColl = tmpRoot.WebApplication.Sites; //objects for the CSV file generation StreamWriter SW; SW = File.AppendText("c:\\WorkflowReport.csv"); //Write the CSV Header SW.WriteLine("Site Collection Name, Site Name, Site URL, List Name, List URL, Workflow Name, Running Instances"); //Enumerate through each site collection foreach (SPSite tmpSite in tmpRootColl) { //Enumerate through each sub-site foreach (SPWeb tmpWeb in tmpSite.AllWebs) { //Enumerate through each List foreach (SPList tmpList in tmpWeb.Lists) { //Enumerate through associated workflows foreach(SPWorkflowAssociation tmpSPWorkflowAssociation in tmpList.WorkflowAssociations) { // If you want to Limit to SPD workflows: check tmpSPWorkflowAssociation.BaseTemplate == null //Skip the Previous versons of workflows if(!tmpSPWorkflowAssociation.Name.Contains("Previous Version")) { //Remove the , in Site Name, document names and write them into the CSV file SW.WriteLine(tmpSite.RootWeb.Title.Replace(",", " ") + "," + tmpWeb.Title.Replace(",", " ") + "," + tmpWeb.Url + "," + tmpList.Title.Replace(",", " ") + "," + tmpWeb.Url + "/" + tmpList.RootFolder.Url + "," + tmpSPWorkflowAssociation.Name.Replace(",", " ") + "," + tmpSPWorkflowAssociation.RunningInstances); } } } } } //Close the CSV file object SW.Close(); //Dispose of the Root Site Object tmpRoot.Dispose(); Console.WriteLine(@"Report Generated Successfull at c:\WorkflowReport.csv. Press ""Enter"" key to Exit"); //Just to Pause Console.ReadLine(); } catch (Exception ex) { //Log the exception to event log System.Diagnostics.EventLog.WriteEntry("Get Workflow Report", ex.Message); } } } }and the output goes like this:
Update:
Another workflow Report:This time the requirement is: Get me the workflows for all the lists and Libraries with No. of workflows completed, In Progress, Terminated, Cancelled.
Alright, I changed the code slightly:
class GetWorkflowsReport { static void Main(string[] args) { string site; try { if (args.Length == 0) { Console.WriteLine("Enter the Web Application URL:"); site = Console.ReadLine(); } else { site = args[0]; } SPSite tmpRoot = new SPSite(site); SPSiteCollection tmpRootColl = tmpRoot.WebApplication.Sites; //objects for the CSV file generation StreamWriter SW; SW = File.AppendText("c:\\WorkflowReportEx.csv"); //Write the CSV Header SW.WriteLine("Site Collection Name, Site Name, Site URL, List Name, List URL, Cancelled,Completed, Faulting,Running,Terminated"); //Enumerate through each site collection foreach (SPSite tmpSite in tmpRootColl) { //Enumerate through each sub-site foreach (SPWeb tmpWeb in tmpSite.AllWebs) { //Enumerate through each List foreach (SPList tmpList in tmpWeb.Lists) { //Initialise the variables int Cancelled=0; int Completed=0; int Faulting=0; int Running=0; int Terminated=0; if (tmpList.WorkflowAssociations.Count > 0) //IF the List has any workflows associated { foreach (SPListItem oSPListItem in tmpList.Items) { foreach (SPWorkflow oSPWorkflow in oSPListItem.Workflows) { //Get the state of the workflow, Whether completed, Terminated, etc switch (oSPWorkflow.InternalState) { case SPWorkflowState.Cancelled: Cancelled++; ; break; case SPWorkflowState.Completed: Completed++; break; case SPWorkflowState.Faulting: Faulting++; break; case SPWorkflowState.Running: Running++; break; case SPWorkflowState.Terminated: Terminated++; break; } } } //Remove the , in Site Name, etc and write them into the CSV file SW.WriteLine(tmpSite.RootWeb.Title.Replace(",", " ") + "," + tmpWeb.Title.Replace(",", " ") + "," + tmpWeb.Url + "," + tmpList.Title.Replace(",", " ") + "," + tmpWeb.Url + "/" + tmpList.RootFolder.Url + "," +Cancelled + "," +Completed+ "," + Faulting + "," + Running+ "," +Terminated); } } } } //Close the CSV file object SW.Close(); //Dispose of the Root Site Object tmpRoot.Dispose(); Console.WriteLine(@"Report Generated Successfull at c:\WorkflowReportEx.csv. Press ""Enter"" key to Exit"); //Just to Pause Console.ReadLine(); } catch (Exception ex) { //Log the exception to event log System.Diagnostics.EventLog.WriteEntry("Get Workflow Report Extended", ex.Message); } }
Nice work, just one query about the second report
ReplyDeleteit is gathering information for the last time each workflow was run against each item, is it possible to get information about everytime a workflow is run against each item? not just the last instance of the workflow an item?
If you looking for the Workflow's track on a particular item, you can Get it from Workflow History lists. This lists keeps the track of all the workflow instances with its details.
DeleteEven the Workflow clean up timer job, just dis-associates the History item from the list and not actually deletes it.
nice one rajack....its very help full to me.
ReplyDeletenice one....its very helpfull to me
ReplyDeleteExcellent piece of work !!
ReplyDelete