-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkflowUtility.cs
105 lines (95 loc) · 4.81 KB
/
WorkflowUtility.cs
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
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.WorkflowServices;
namespace TestingApp
{
public static class WorkflowUtility
{
public static void GetWorkflowReport(string listName, List oList, ClientContext clientContext)
{
try
{
//Get all list items
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =
"<View><Query>" +
"<Where>" +
"<And>" +
"<Neq><FieldRef Name=\"Stage\"/><Value Type=\"Text\">Complete</Value></Neq>" +
"<Neq><FieldRef Name=\"Stage\"/><Value Type=\"Text\">Draft</Value></Neq>" +
"</And>" +
"</Where>" +
// "<OrderBy><FieldRef Name=\"Modified\" Ascending=\"FALSE\"/></OrderBy>" +
"</Query>" +
//"<RowLimit>5000</RowLimit>" +
"</View>";
ListItemCollection collListItem = oList.GetItems(camlQuery);
clientContext.Load(collListItem,
items => items.Include(
item => item.Id,
item => item["Title"],
item => item.ParentList));
clientContext.ExecuteQuery();
List<CSVOutput> list = new List<CSVOutput>();
foreach (ListItem oListItem in collListItem)
{
WorkflowInstanceCollection allinstances = WorkflowExtensions.GetWorkflowInstances(oList.ParentWeb, oListItem);
//clientContext.Load(allinstances);
//clientContext.ExecuteQuery();
foreach (WorkflowInstance instance in allinstances)
{
if (instance.Status == WorkflowStatus.Suspended || instance.Status == WorkflowStatus.Terminated )
{
WorkflowSubscription subscription = WorkflowExtensions.GetWorkflowSubscription(oList.ParentWeb, instance.WorkflowSubscriptionId);
string itemUrl = String.Format(@"{0}/Lists/{1}/DispForm.aspx?ID={2}", oList.ParentWeb.Url, listName, oListItem.Id);
string WorkflowUrl = String.Format(@"{0}/_layouts/15/workflow.aspx?List={1}&ID={2}", oList.ParentWeb.Url, oList.Id, oListItem.Id); ;
Console.WriteLine(string.Format("Item ID:{0}, Title:{1}, Item URL:{2} ", oListItem.Id, oListItem["Title"], itemUrl));
Console.Write(string.Format("Workflow Name:{0},Workflow Status:{1}, message:{2}, Workflow URL: {3} ", subscription.Name, instance.Status, instance.FaultInfo,WorkflowUrl));
CSVOutput item = new CSVOutput(oListItem.Id.ToString(), oListItem["Title"].ToString(), itemUrl, subscription.Name, instance.Status.ToString(), instance.FaultInfo, WorkflowUrl);
list.Add(item);
}
}
}
WriteCSV(list);
}
catch (Exception ex)
{
throw ex;
}
}
static void WriteCSV(List<CSVOutput> list)
{
string filepath = ConfigurationManager.AppSettings["csvOutputPath"]==null?string.Empty:ConfigurationManager.AppSettings["csvOutputPath"];
if (!string.IsNullOrEmpty(filepath))
{
var fileName = filepath + "WorkflowReport " + DateTime.Now.ToString("dd-MMM-yyyy hh-mm tt") + ".csv";
using (CsvFileWriter writer = new CsvFileWriter(fileName))
{
// Write sample data to CSV file
string[] str = { "ItemId", "ItemTitle", "ItemUrl", "Workflow Name",
"Workflow Status","Workflow Message","Workflow URL" };
CsvRow row = new CsvRow();
row.AddRange(str);
writer.WriteRow(row);
foreach (CSVOutput item in list)
{
row = new CsvRow{};
row.Add(item.ItemId);
row.Add(item.ItemTitle);
row.Add(item.ItemUrl);
row.Add(item.WorkflowName);
row.Add(item.WorkflowStatus);
row.Add(item.WorkflowMessage);
row.Add(item.WorkflowURL);
writer.WriteRow(row);
}
}
}
}
}
}