Wouldn’t it be nice to start a specific SharePoint list workflow on each item in a list? Unfortunately, there’s no simple way to do this out of the box in SharePoint or Nintex, even though it’s just a few lines of code. You could always grab all of the list items and then loop through each one and start a workflow, but this is complicated and cumbersome.
Using Nintex Workflow 2010, it’s incredibly easy to create a simple to use and configure workflow action that can start any workflow associated with a list. I’m not going to go into detail on how to create a Nintex action, their help document does a pretty good job, but I will say you need to have the Nintex Workflow SDK installed in order to create an action. Once you have your action created, there are two interesting items left to complete: your action’s Execute method and the custom dialog. Everything else from the default action process remains the same.
Custom Execute Method
The default Execute action is rather bland, but has a ton of potential. The Execute method is where the magic happens, and you can do anything the user could do. That’s something to keep in mind, because the same security still applies, unless your workflow is run from the farm account, but you shouldn’t count on that.
Anyway, you want to grab the List ID that the workflow author provided along with the Workflow Name and any startup data that the workflow will need. These are all strings, because that’s what you get back from all of the Nintex configuration controls. Since my version of the Workflow Data allows the user to provide anything in the XML, you need to resolve the context data before using it. Once you have the List ID, Workflow Name, and Workflow Data, you’re ready to go. Just grab the list, get the corresponding WorkflowAssociation, and loop through each item starting the WorkflowAssociation on the item.
One thing I want to point out is you can start the WorkflowAssociation synchronously or asynchronously. I have the workflow starting synchronously so if one item fails, my action fails. The choice is up to you and asynchronous will be faster generally.
Without further ado, do the code:
Everything that happens inside the for loop can by changed for your purposes to suit any sort of looping you want. You don’t have to start a workflow, you could use this construct to change every item in a list.
Here, we’re going to provide the user with a list of the Lists in the current site. Based on the List selected, we’ll then show a list of applicable WorkflowAssociations to choose from. On the backend, we’ll be storing the List’s ID and the WorkflowAssociation’s Name, because each is unique. The last thing we need the user to provide is the Workflow Data. It’s possible to pull the required workflow fields from the workflow, but I haven’t for the sake of brevity and showing what the data actually looks like. Note that no validation occurs in the custom dialog.
To the code:
There you have it: a Nintex Workflow action that will start a selected workflow on every list item for a given list. The code isn’t all that hard, once you know what you’re doing in Nintex. It’s all about passing elements back and forth successfully and getting the right data to the right place. Now that some of the effort is demystified, what will you make next?