Mostrando entradas con la etiqueta Workflow. Mostrar todas las entradas
Mostrando entradas con la etiqueta Workflow. Mostrar todas las entradas

jueves, 29 de septiembre de 2016

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.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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);
            }
        }

and the output :


Read more: http://www.sharepointdiary.com/2012/02/workflow-reports-in-sharepoint.html#ixzz4LeXGXsYC