Programmatically Get The Supended/Terminated WorkFlow Details Using CSOM

The code given below will execute in the site level and retrieve all the suspended Workflows programamtically.
  1. Open your Visual Studio.
  2. Select the console Application.
  3. Copy and paste the code given below. 
  1. using Microsoft.SharePoint.Client;  
  2. using Microsoft.SharePoint.Client.WorkflowServices;  
  3. using System;  
  4. using System.Linq;  
  5. using System.Security;  
  6.   
  7. namespace GowthamWFTutorial  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             
  14.             string list = "TutorialWFList";  
  15.             string workflowName = "TestWorkflow";  
  16.             var workflowStatus = WorkflowStatus.Suspended;   
  17.             var Context = SP.ClientContext.get_current();  
  18.             Web web = Context.Web;  
  19.             Guid listGUID= GetListGUID(Context, list);  
  20.   
  21.               
  22.             var wfServicesManager = new WorkflowServicesManager(Context,web);  
  23.             var wfSubscriptionService = wfServicesManager.GetWorkflowSubscriptionService();  
  24.             var wfSubscriptions = wfSubscriptionService.EnumerateSubscriptionsByList(listGUID);  
  25.             clientContext.Load(wfSubscriptions, wfSubs => wfSubs.Where(wfSub => wfSub.Name == workflowName));  
  26.             clientContext.ExecuteQuery();  
  27.             var wfSubscription = wfSubscriptions.First();  
  28.   
  29.               
  30.             var wfInstanceService = wfServicesManager.GetWorkflowInstanceService();  
  31.             var wfInstanceCollection = wfInstanceService.Enumerate(wfSubscription);  
  32.             clientContext.Load(wfInstanceCollection, wfInstances => wfInstances.Where(wfI => wfI.Status == workflowStatus));  
  33.             clientContext.ExecuteQuery();  
  34.   
  35.               
  36.             foreach (var wfInstance in wfInstanceCollection)  
  37.             {  
  38.                 Console.WriteLine(wfInstance.Properties["Microsoft.SharePoint.ActivationProperties.CurrentItemUrl"]);  
  39.             }  
  40.   
  41.             Console.ReadKey();  
  42.         }  
  43.   
  44.   
  45.         private static Guid GetListGUID(ClientContext context, string listTitle)  
  46.         {  
  47.             var list = context.Web.Lists.GetByTitle(listTitle);  
  48.             context.Load(list, l => l.Id);  
  49.             context.ExecuteQuery();  
  50.             return list.Id;  
  51.         }  
  52.     }  
  53. }   
Thanks for reading my blog. 
Ebook Download
View all
Learn
View all