0

I am able to get all the workflows associated with a list item. But it looks like only one of the workflows can be running for the list item in this list. I want to be able to figure out which is the running workflow. This is the code snippet with which I am displaying all the properties of a WorkflowInstance.

int listItemId = 6326;
var workflowInstances = wfInstanceService.EnumerateInstancesForListItem(listGUID, listItemId);
clientContext.Load(workflowInstances);
clientContext.ExecuteQuery();
foreach (var instance in workflowInstances)
{
    PrintText("Instance ID: " + instance.Id.ToString(), sw);
    PrintText("Status: " + instance.Status.ToString(), sw);
    PrintText("Creation time: " + instance.InstanceCreated.ToString(), sw);
    PrintText("Last updated time: " + instance.LastUpdated.ToString(), sw);
    PrintText("User status: " + instance.UserStatus, sw);

    IDictionary<string, string> props = instance.Properties;
    foreach(KeyValuePair<string, string> kvp in props)
    {
        PrintText(kvp.Key+": "+kvp.Value, sw);
    }

    PrintText("---------", sw);
}

Say there are 5 workflows associated with the list item 6326, I see all the details printing properly. But, I am not able to find out which one is running with all the data that I'm printing above. (PrintText function is essentially a Console.WriteLine). In my list items I cannot assume that the workflow that was created most recent is the running one too.

Link to where I found these built in functions: https://docs.microsoft.com/en-us/sharepoint/dev/general-development/working-with-the-sharepoint-workflow-services-client-side-object-model

How do I determine which one is running?

  • Finished workflows should have the value of Status field set to "Completed". – Damjan Tomic Jun 25 '18 at 9:38
0

Each Workflow Instance should have a status which will be a value in the WorkflowStatus enumeration: https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.workflowservices.workflowstatus.aspx?f=255&MSPPError=-2147217396

To get the running workflow instances, you could check for the "Started" status:

string siteUrl = "https://tenant.sharepoint.com/sites/intranet";
string userName = "admin@tenant.onmicrosoft.com";
string password = "adminpassword";
string listTitle = "MyList";
string workflowName = "MyWorkflow";
var workflowStatus = WorkflowStatus.Started;
var clientContext = GetClientContext(siteUrl, userName, password);
Web web = clientContext.Web;
Guid listGUID= GetListGUID(clientContext, listTitle);

//Get the workflow subscription (workflow definition)
var wfServicesManager = new WorkflowServicesManager(clientContext, 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();

//Get the workflow instances
var wfInstanceService = wfServicesManager.GetWorkflowInstanceService();
var wfInstanceCollection = wfInstanceService.Enumerate(wfSubscription);
clientContext.Load(wfInstanceCollection, wfInstances => wfInstances.Where(wfI => wfI.Status == workflowStatus));
clientContext.ExecuteQuery();

//List all instances with the selected status. 
foreach (var wfInstance in wfInstanceCollection)
{
    Console.WriteLine(wfInstance.Properties["Microsoft.SharePoint.ActivationProperties.CurrentItemUrl"]);
}

I have blogged about get all workflow instances with a specific status here: https://www.vrdmn.com/2016/09/get-all-suspended-or-terminated.html

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.