The code given below will execute in the site level and retrieve all the suspended Workflows programamtically.
- Open your Visual Studio.
- Select the console Application.
- Copy and paste the code given below.
- using Microsoft.SharePoint.Client;
- using Microsoft.SharePoint.Client.WorkflowServices;
- using System;
- using System.Linq;
- using System.Security;
-
- namespace GowthamWFTutorial
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- string list = "TutorialWFList";
- string workflowName = "TestWorkflow";
- var workflowStatus = WorkflowStatus.Suspended;
- var Context = SP.ClientContext.get_current();
- Web web = Context.Web;
- Guid listGUID= GetListGUID(Context, list);
-
-
- var wfServicesManager = new WorkflowServicesManager(Context,web);
- var wfSubscriptionService = wfServicesManager.GetWorkflowSubscriptionService();
- var wfSubscriptions = wfSubscriptionService.EnumerateSubscriptionsByList(listGUID);
- clientContext.Load(wfSubscriptions, wfSubs => wfSubs.Where(wfSub => wfSub.Name == workflowName));
- clientContext.ExecuteQuery();
- var wfSubscription = wfSubscriptions.First();
-
-
- var wfInstanceService = wfServicesManager.GetWorkflowInstanceService();
- var wfInstanceCollection = wfInstanceService.Enumerate(wfSubscription);
- clientContext.Load(wfInstanceCollection, wfInstances => wfInstances.Where(wfI => wfI.Status == workflowStatus));
- clientContext.ExecuteQuery();
-
-
- foreach (var wfInstance in wfInstanceCollection)
- {
- Console.WriteLine(wfInstance.Properties["Microsoft.SharePoint.ActivationProperties.CurrentItemUrl"]);
- }
-
- Console.ReadKey();
- }
-
-
- private static Guid GetListGUID(ClientContext context, string listTitle)
- {
- var list = context.Web.Lists.GetByTitle(listTitle);
- context.Load(list, l => l.Id);
- context.ExecuteQuery();
- return list.Id;
- }
- }
- }
Thanks for reading my blog.