/// <summary> /// Show all running workflow instances /// </summary> public static void ListAllInstances(ref ClientContext clientConext, ref WorkflowServicesManager wfServicesManager, Guid listId) { WorkflowInstanceService instService = wfServicesManager.GetWorkflowInstanceService(); Console.WriteLine(); Console.WriteLine("Show all running workflow instances..."); int listItemId = 1; WorkflowInstanceCollection wfInstances = instService.EnumerateInstancesForListItem(listId, listItemId); // WorkflowInstanceCollection wfInstances = instService.EnumerateInstancesForSite(); // get instances running on the current site clientConext.Load(wfInstances); clientConext.ExecuteQuery(); foreach (var wfInstance in wfInstances) { Console.WriteLine("{0} - {1} - {2}|{3}", wfInstance.Id, wfInstance.LastUpdated, wfInstance.Status, wfInstance.UserStatus); } }
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; } }
/// <summary> /// Return a single workflow instance. /// </summary> public static WorkflowInstance GetOneRunningInstance(ref ClientContext clientConext, ref WorkflowServicesManager wfServicesManager, Guid listId) { WorkflowInstanceService instService = wfServicesManager.GetWorkflowInstanceService(); int listItemId = 1; WorkflowInstanceCollection wfInstances = instService.EnumerateInstancesForListItem(listId, listItemId); clientConext.Load(wfInstances); clientConext.ExecuteQuery(); return(wfInstances.FirstOrDefault()); }
private static bool CancelWorkflowOnListItem(SPListItem item, List <Guid> workflowDefinitionIds) { var result = false; try { Guid siteId = item.Web.Site.ID; Guid webId = item.Web.ID; SPSecurity.RunWithElevatedPrivileges(delegate { using (SPSite site = new SPSite(siteId)) { using (SPWeb web = site.OpenWeb(webId)) { // get our manager and services var workflowServicesManager = new WorkflowServicesManager(web); if (!workflowServicesManager.IsConnected) { throw new NotConnectedException(); } var workflowSubscriptionService = workflowServicesManager.GetWorkflowSubscriptionService(); var workflowInstanceService = workflowServicesManager.GetWorkflowInstanceService(); // get our subscription WorkflowSubscription workflowSubscription = default(WorkflowSubscription); var workflowSubscriptionCollection = workflowSubscriptionService.EnumerateSubscriptionsByList(item.ParentList.ID); foreach (var wfSubscription in workflowSubscriptionCollection) { foreach (Guid inputId in workflowDefinitionIds) { if (wfSubscription.DefinitionId.Equals(inputId)) { workflowSubscription = wfSubscription; break; } } if (workflowSubscription != null) { break; } } if (workflowSubscription == null) { throw new Exception("Could not load subscription. Please ensure workflow is attached to list: " + item.ParentList.RootFolder.ServerRelativeUrl); } var itemGuid = Guid.Empty; if (item.Fields.ContainsField("GUID") && item["GUID"] != null) { itemGuid = new Guid(item["GUID"].ToString()); } var listGuid = Guid.Parse(workflowSubscription.GetProperty("Microsoft.SharePoint.ActivationProperties.ListId")); WorkflowInstanceCollection instances = workflowInstanceService.EnumerateInstancesForListItem(listGuid, item.ID); if (instances != null && instances.Count > 0) { foreach (var instance in instances) { workflowInstanceService.CancelWorkflow(instance); } } result = true; } } }); } catch (Exception err) { result = false; } return(result); }