Reviving SharePoint Designer Workflow From Suspended Status, Programmatically

Introduction

This article helps to get you started by explaining some key design considerations and providing a basic procedure. In this article, we will see about SharePoint Designer Workflow, when it is in suspended status, and how we can retrieve it programmatically. In the last section, you can find the links to complete Workflow examples.

Why we are using SharePoint designer Workflow?

We can design Workflows that add an Application logic to your site or an Application without writing a custom code.

Types of workflows

All SharePoint Workflows are associated with a SharePoint list/site. We have three different types of Workflows which can start and are given below.

  • List Workflow
  • Reusable Workflow
  • Site Workflow

Before doing a  deep dive on Workflows, first we will see about how to work on SharePoint Designer Workflows.

Create a SharePoint 2013 Designer Workflow steps

  1. Click Workflows section in the top navigation.
  2. We have three different Workflows. Select anyone -> List Workflowdrop-down in the New section of the ribbon.
  3. Choose the list with which we want to attach it.


  1. Enter the basic details. Create List Workflow dialog box and then make sure that the platform type is set to SharePoint 2013 Workflow.


The procedure given above is used to create a List Workflow. A reusable Workflow or site Workflow can be created, using the same procedure with the modification given below.

Suspend/terminate Workflows automatically

Sometimes Workflows cannot start and it has been terminated/suspended automatically. We can see it in a Workflow settings page, see the status for the initiated Workflow, as suspended or canceled.

Example

This is the sample issue listed but many errors can occur, like the status given below.

The Workflow Information page displays an error message that resembles what is shown below.

See the error message given below.

RequestorId: ac01a77b-619a-dbd2-0000-000000000000.

Details

An unhandled exception occurred during the execution of the Workflow instance.

Exception details 

  1. System.ApplicationException: HTTP 401 {  
  2.     "Transfer-Encoding": ["chunked"],  
  3.     "X-SharePointHealthScore": ["0"],  
  4.     "SPClientServiceRequestDuration": ["864"],  
  5.     "SPRequestGuid": ["ac01a77b-619a-dbd2-aa49-a4d2580be234"],  
  6.     "request-id": ["ac01a77b-619a-dbd2-aa49-a4d2580be234"],  
  7.     "X-FRAME-OPTIONS": ["SAMEORIGIN"],  
  8.     "MicrosoftSharePointTeamServices": ["15.0.0.4631"],  
  9.     "X-Content-Type-Options": ["nosniff"],  
  10.     "X-MS-InvokeApp": ["1; RequireReadOnly"],  
  11.     "Cache-Control": ["max-age=0, private"],  
  12.     "Date": ["Mon, 12 Jan 2015 20:03:16 GMT"],  
  13.     "Server": ["Microsoft-IIS\/8.5"],  
  14.     "WWW-Authenticate": ["NTLM"],  
  15.     "X-AspNet-Version": ["4.0.30319"],  
  16.     "X-Powered-By": ["ASP.NET"]  
  17. }  
  18. at Microsoft.Activities.Hosting.Runtime.Subroutine  
  19. SubroutineChild.Execute(CodeActivityContext context) at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager) at  
  20. System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)   

The code given below will execute in the site level and retrieve all the suspended Workflows programmatically.

  1. Open your Visual Studio.
  2. Select the console Application.
  3. Copy and paste the code given below.

Source code

  1. using Microsoft.SharePoint.Client;  
  2. using Microsoft.SharePoint.Client.WorkflowServices;  
  3. using System;  
  4. using System.Linq;  
  5. using System.Security;  
  6. namespace GowthamWFTutorial {  
  7.     class Program {  
  8.         static void Main(string[] args) {  
  9.             string list = "TutorialWFList";  
  10.             string workflowName = "TestWorkflow";  
  11.             var workflowStatus = WorkflowStatus.Suspended;  
  12.             var Context = SP.ClientContext.get_current();  
  13.             Web web = Context.Web;  
  14.             Guid listGUID = GetListGUID(Context, list);  
  15.             var wfServicesManager = new WorkflowServicesManager(Context, web);  
  16.             var wfSubscriptionService = wfServicesManager.GetWorkflowSubscriptionService();  
  17.             var wfSubscriptions = wfSubscriptionService.EnumerateSubscriptionsByList(listGUID);  
  18.             clientContext.Load(wfSubscriptions, wfSubs => wfSubs.Where(wfSub => wfSub.Name == workflowName));  
  19.             clientContext.ExecuteQuery();  
  20.             var wfSubscription = wfSubscriptions.First();  
  21.             var wfInstanceService = wfServicesManager.GetWorkflowInstanceService();  
  22.             var wfInstanceCollection = wfInstanceService.Enumerate(wfSubscription);  
  23.             clientContext.Load(wfInstanceCollection, wfInstances => wfInstances.Where(wfI => wfI.Status == workflowStatus));  
  24.             clientContext.ExecuteQuery();  
  25.             foreach(var wfInstance in wfInstanceCollection) {  
  26.                 Console.WriteLine(wfInstance.Properties["Microsoft.SharePoint.ActivationProperties.CurrentItemUrl"]);  
  27.             }  
  28.             Console.ReadKey();  
  29.         }  
  30.         private static Guid GetListGUID(ClientContext context, string listTitle) {  
  31.             var list = context.Web.Lists.GetByTitle(listTitle);  
  32.             context.Load(list, l => l.Id);  
  33.             context.ExecuteQuery();  
  34.             return list.Id;  
  35.         }  
  36.     }  
  37. }  

You cannot change which list a Workflow is attached to after you save the Workflow. Thus, we have to create a new Workflow and attach it to the list which you want.

Conclusion

This article explains about the basic creation of Workflows and how to get the suspended/terminated Workflows, using Client Object model. By now, you must have understood the key concepts and design considerations, so you might want to design a Workflow, which has a specific application.