2

I want to get the running status of SharePoint 2013 workflow in my event receiver. I am trying to use the below code but its not working :

public static bool IsWorkflowRunning(SPListItem listItem, Guid workflowId)
{
    foreach (SPWorkflow workflow in listItem.Workflows)
    {
        if (workflow.ParentAssociation.BaseTemplate.Id == workflowId && workflow.InternalState == SPWorkflowState.Running)
            {
                return true;
            }
    }
    return false;
}

am trying to trigger the SharePoint designer 2013 workflow that's deployed in a site collection from an event receiver. and when I debugged the event receiver and perform some updates[ listitem.update() ] , it will trigger the workflow again, but as per my requirement I need to trigger the SP D 2013 WORKFLOW only once. Hence,for achieving this I want to check the internal status of the workflow and if its running, I will not trigger the workflow again.

the above code i have written is , it seems from the SP 2010 WF API. it doesnt work for the workflows thats been created using Workflow Manager infrastructure in SP 2013.

3

If you want to get SharePoint 2013 Workflow (subscription) you need to get it from WorkflowInstanceService and not from SPListItem. This is what I did to get instance of that Workflow and check its status.

You will need to have this reference Microsoft.SharePoint.WorkflowServicesBase and using Microsoft.SharePoint.WorkflowServices; directive.

public void IsWorkflowRunning(string workflowName, Guid listId, int listItemId)
{
   var workflowServiceManager = WorkflowServicesManager.Current;
   var workflowInstanceService = workflowServiceManager.GetWorkflowInstanceService();
   foreach (var workflowInstance in workflowInstanceService.EnumerateInstancesForListItem(listId, listItemId)){
      var workflowSubscription = workflowServiceManager.GetWorkflowSubscriptionService().GetSubscription(workflowInstance.WorkflowSubscriptionId);
      if (workflowSubscription.Name == workflowName && workflowInstance.Status != WorkflowStatus.Terminated)//and another status workflow Cancelled, Completed
         return true;
   }
   return false;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.