Forum Discussion

Alexander Korablinov's avatar
Alexander Korablinov
Copper Contributor
Jul 07, 2017

How do I get workflow instances for list item on the client side

Hi,

Question: What client side API can I use to retrieve all workflow instances for SharePoint list item?

 

Environment:
SharePoint 2013/2016 with SharePoint 2010 Workflow platform, i.e. Workflow Manager is NOT installed.
Nintex Workflow

 

I have the following:
1.    SharePoint site URL
2.    List name
3.    Workflow name
4.    Item ID

 

I have already tried the following approaches:
1.    WorkflowLog[] NintexWorkflowService.GetWorkflowHistoryForListItem(int itemId, string listName, SPWorkflowState stateFilter, string workflowNameFilter)
http://help.nintex.com/en-US/sdks/sdk2013/Reference/SOAP/NW_REF_SOAP_GetWorkflowHistoryForListItem.htm
This method is not reliable because it may return null even if workflow instance exists.
2.   CSOM WorkflowInstanceService.EnumerateInstancesForListItem(Guid, Int32)
The following C# example does not work:
ClientContext clientContext = new ClientContext(siteUrl);
var workflowServicesManager = new SP.WorkflowServices.WorkflowServicesManager(clientContext, clientContext.Web);
var workflowInstanceService = workflowServicesManager.GetWorkflowInstanceService();
var workflowInstances = workflowInstanceService.EnumerateInstancesForListItem(new Guid("{f79ba745-ffe8-4e77-b3a4-3f37ab7f1b0d}"), 100);
clientContext.Load(workflowInstances);
clientContext.ExecuteQuery();

 

ExecuteQuery throws an exception: Cannot invoke method or retrieve property from null object. Object returned by the following call stack is null. "GetWorkflowInstanceService
new Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager()"

 

I will appreciate any help.

  • Russell Gove's avatar
    Russell Gove
    Iron Contributor
    This is working for me
    let listId = await this.getListId(this.properties.technicalRequestListName);
    var context = SP.ClientContext.get_current();
    var workflowServicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, context.get_web());
    var workflowInstanceService = workflowServicesManager.getWorkflowInstanceService();
    var workflowDeploymentService = workflowServicesManager.getWorkflowDeploymentService();
    //Get all the definitions from the Deployment Service, or get a specific definition using the GetDefinition method.
    let wfDefinition:SP.WorkflowServices.WorkflowDefinition = (await this.getWorkFlowByName(workflowDeploymentService, workflowName));
    let wfDefinitionId: string = (await this.getWorkFlowByName(workflowDeploymentService, workflowName)).get_id();
    let wfInstances:SP.WorkflowServices.WorkflowInstanceCollection = workflowInstanceService.enumerateInstancesForListItem(listId, ItemId);
    context.load(wfInstances);
    context.executeQueryAsync(
    (sender, args) => {
    var instancesEnum = wfInstances.getEnumerator();
    • Alexander Korablinov's avatar
      Alexander Korablinov
      Copper Contributor
      Hi Dean, thank you for this option, but it does not look like a client side solution.
      If I chose between server side solutions, I would prefer some class liblary like SharePoint 2013 Core server
  • For SP 2010 workflows there is not a client side API you can use...you can only server side API...approach 2 is only valid for SP 2013 / 2016 workflows (deployed to Windows Azure Workflow)
    • Alexander Korablinov's avatar
      Alexander Korablinov
      Copper Contributor

      Hi, thank you for the answer.

      I created a new web service and deployed it on the SharePoint server. My new service has only one method GetWorkflowsForListItem. Basically it wraps SPListItem.Workflows property.

      MSDN says about this property: Gets the collection of the workflow objects that represent instances of the item workflows that are currently running, but does not get the workflow associations.

      Could you please clarify: "currently running" - does it return only workflows with InternalState == SPWorkflowState.Running?

      I need all workflows regardless their state.

Resources