Advertisement

Responsive Advertisement

SharePoint 2013 Suspended/Terminated Workflow details programmatically using CSOM



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 suspended status, how we can retrieve it programmatically. In the last section, you can find links to complete workflow examples.

Why we are using SharePoint designer Workflow?
We can design workflows that add application logic to your site or application without having to write custom code

Types Of workflows,

All SharePoint Workflows are associated with a SharePoint list/Site .We have three different types of workflow can start.

  • 1.List Workflow
  • 2.Reusable Workflow
  • 3.Site Workflow

Before going to deep dive on the workflows ,first we will see about how to work on sharepoint designer workflows.
Create a SharePoint 2013 Designer Workflow Steps:

  1. Click the Workflows section in the top Navigation
  2. We have three different workflows select anyone -> List Workflow drop-down in the New section of the ribbon.
  3. Choose the list which we you 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 above procedure is used to create a List workflow. A Reusable workflow or Site workflow can be created using the same procedure with the following modification.

Suspend/terminate workflows automattically

But 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 can occur like the below status,

The Workflow Information page displays an error message that resembles the following:
See the Below Error message;

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

Details:
An unhandled exception occurred during the execution of the workflow instance.
Exception details:
System.ApplicationException: HTTP 401 {"Transfer-Encoding":["chunked"],"X-SharePointHealthScore":["0"],"SPClientServiceRequestDuration":["864"],"SPRequestGuid":["ac01a77b-619a-dbd2-aa49-a4d2580be234"],
"request-id":["ac01a77b-619a-dbd2-aa49-a4d2580be234"],"X-FRAME-OPTIONS":["SAMEORIGIN"],"MicrosoftSharePointTeamServices":["15.0.0.4631"],
"X-Content-Type-Options":["nosniff"],"X-MS-InvokeApp":["1; RequireReadOnly"],"Cache-Control":["max-age=0, private"],
"Date":["Mon, 12 Jan 2015 20:03:16 GMT"],"Server":["Microsoft-IIS\/8.5"],"WWW-Authenticate":["NTLM"],"X-AspNet-Version":["4.0.30319"],
"X-Powered-By":["ASP.NET"]} at Microsoft.Activities.Hosting.Runtime.Subroutine

SubroutineChild.Execute(CodeActivityContext context) at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance,
ActivityExecutor executor, BookmarkManager bookmarkManager) at
System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

Below code will execute in the site level and retrieve the all suspended workflows programmatically.


1.open your Visual studio

2.Select the console Application.

3.Copy and Paste the below code.

Source Code
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;
       }
   }
}


You cannot change which list a workflow is attached to after you save the workflow. So we have to create a new workflow and attach it to the list that you want.

Conclusion:

This article explains about basic creation of workflows and how to get the suspended/terminated workflows using client object model. Now that you understand the key concepts and design considerations, you might want to design a workflow that has a specific application.

Post a Comment

0 Comments