cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Workflow Hero

How to Trigger List Level Workflow on each item programmatically using CSOM?

Need to Start workflow for every item in list for once, which generally triggers on item edit. Need a CSOM in C# to do the. SharePoint 2013 on premises environment and Nintex Workflows 2013.

Code Example :- Following didn't worked.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.WorkflowServices;

namespace ItemLevelWorkflowTrigger
{
class Program
{
static void Main(string[] args)
{
try
{
var workflowID = "0b691638-225c-11ad-b52f-22e4eb7686aA";
var siteCollectionUrl = "https://xyz.com/collection/subsite";
var clientContext = new ClientContext(siteCollectionUrl);
clientContext.Credentials = new NetworkCredential("xyz", "PQR@123", "abc");
SP.List oList = clientContext.Web.Lists.GetByTitle("List Name");

CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><Where><Eq><FieldRef Name='PDStatus'/>" +
"<Value Type='Choice'>Published</Value></Eq></Where></View>";

ListItemCollection collListItem = oList.GetItems(camlQuery);
clientContext.Load(collListItem);
clientContext.ExecuteQuery();

var workflowServicesManager = new WorkflowServicesManager(clientContext, clientContext.Web);
clientContext.Load(workflowServicesManager);
clientContext.ExecuteQuery();

var workflowSubscriptionService = workflowServicesManager.GetWorkflowSubscriptionService().EnumerateSubscriptionsByDefinition(new Guid(workflowID));
clientContext.Load(workflowSubscriptionService);
clientContext.ExecuteQuery();


var subsEnum = workflowSubscriptionService.GetEnumerator();
while (subsEnum.MoveNext())
{
var subs = subsEnum.Current;
int count = 1;
foreach (ListItem oListItem in collListItem)
{
IDictionary<string, object> dictionary = new Dictionary<string, object>() { };
workflowServicesManager.GetWorkflowInstanceService().StartWorkflowOnListItem(subs, oListItem.Id, dictionary);
clientContext.ExecuteQuery();
Console.WriteLine("" + count++);
}
}
Console.WriteLine("Completed");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.ReadLine();
}
}
}
}

Labels: (1)
0 Kudos
Reply