K2’s 4.6.11 – For Each Wizard

With the release of K2 4.6.11 a new wizard has been added to the k2 blackpearl software. It’s the For Each wizard, which might surprise you. How can a workflow engine like K2 not have a For Each option? It’s quite simple once you understand how the K2 workflow engine works.

Activities and line rules determine the flow of the process. Questions like ‘Does your workflow have branch and merge options?’ never really applied to K2 due to the easy structure of Activities and Line Rules. Every line rule is evaluated after the activity is completed and the line is followed once the line rule evaluates to True. A loop becomes a simple line back to the same activity with a line rule evaluating to True. A branch is just an activity with two lines that evaluate to True and a Merge is an activity with two lines rules going into it that both evaluate to True, with a preceding rule that checks if the activity should start. If a loop is just a line back to the same activity with some line rules, why add a For Each wizard?

The reason is simple, continuous improvements on ease of use and usability.

Before the For Each wizard we had to use a difficult-to-explain option called a destination set configured as “Plan per slot (no destination)” to loop over a set of items. It’s a destination with no destination – yeah, not very clear. It also didn’t show up very well in View Flow, which is the live graphical report for what’s going on with a particular instance of a workflow. If you wanted to do more advanced activities in that loop, you would have to use an IPC event. This means a sub-process per item in the loop which is also not always the best approach visually as well as for performance.

The For Each wizards solves these with an easy way to loop through a list of items.

How does it work?

K2 Studio and K2 for Visual Studio

Let’s first see how it works in the K2 Studio and/or Visual Studio designers:

For Each event in K2 for Visual Studio

Drag and drop the For Each wizard onto the canvas and you’ll need to specify a list of items to loop through in the Source field. You can loop over various items, like:

  • List Item Reference
  • SmartObject List method
  • Comma separated list

ForEach - Object browser

The wizard also asks for the field Reference and Index. The reference becomes the name of the Item Reference for the items in the list. The index is used for two Data Fields. In the example, the Data Fields are ItemIndex and ItemIndex Result. The Item Index is simply the count of the loop, while the ItemIndex Result tells you if there is a next item or not. You will most likely use the item reference created. The item reference refers to the current item in the loop. If you’ve used a list item reference (created with the Create List Reference), then this would be the full reference to that item and not just a reference with a single variable in it. The For Each wizard add its own item reference, so if you use an item reference in the For Each wizard, you will have two item references, which technically might be a consideration as References are stored as a bit of XML and this impacts performance and database growth slightly. If you’ve used a SmartObject than it would contain the property that you selected in the Source field of the For Each loop. If performance really is key, we would use a SmartObject directly in the For Each wizard and not create an Item Reference first. Obviously working with an item reference (for example, looping over list items) can make life a lot easier for you when designing your workflow.

Once you’ve placed the For Each wizard, you get two lines out of the activity. You must create the activities to which these lines need to go yourself, and when you’re done you create a line back to the For Each activity. The final result would looks something like this:

K2 Designer

Within the K2 Designer, it all works similar to the full client, except that it looks a little different:

What can be confusing is that the K2 Designer most likely already has an item reference associated with the process. The reason is because this designer always has context of an item. In the screenshot, I’ve made my workflow on a custom list in SharePoint called Emails. I initially thought that I could use that, but that is just an item reference, so it’s not a list of items. Within the K2 Designer there is a lot more benefit from a design perspective to first create a reference!

The final result would like this:


The For Each wizard is a nice addition to the K2 platform and allows an easier design of the workflow. It’s quite easy to use (especially in combination with the Create Reference wizard), which is exactly the aim of the wizard.

K2 smartforms custom theme tips

So, it’s been a while since i’ve written something. Yes, i’m still alive – a lot has happened in personal and professional life!
With K2 smartforms already being at release 1.0.4, it’s about time I wrote something about it!

A lot of customers are using a custom theme to create forms that are in their company’s style/look&feel.
There’s an article on how to create a theme, but here’s my version of it:

  • Copy one of the themes in “C:\Program Files (x86)\K2 blackpearl\K2 smartforms Designer\Styles\Themes” to a new folder, which has the name of your theme.
  • Copy the CSS file with the same name, into a new file with the name of the newly copied directory. This will be your theme name.
  • Do the same for the C:\Program Files (x86)\K2 blackpearl\K2 smartforms Runtime\Styles\Themes folder and CSS file.
  • Add the theme name to the K2 database. Insert into the Form.Theme table. Only the name is enough!
  • SmartForms is heavily cached for performance. This cache causes theme changes not to show up. Disable this cache by changing the Forms.CombinedResources.Cache.Enabled value to False in the web.config of both the design and runtime site!
  • Recycle the application pool, or do an IISRESET.

And that’s basically it! If you want to quickly change some colours, there’s also a K2 smartforms theme builder on the K2 underground.

Happy smartforming!

Notes on K2 Inline functions

Yesteday I tweeted about inline functions and how to get your Icon working properly.

Appart from all the info in Johnny’s blogpost, there are some other catches as well. Just because i keep forgetting them, here’s a mental note to myself.


It’s a normal class library

Start your project with a normal .NET class library. Make sure it’s .NET 3.5. Add references to the following files:

  1. SourceCode.Framework
  2. SourceCode.Workflow.Functions – this one isn’t always needed.
  3. SourceCode.Workflow.Functions.Design
  4. System.Drawing

Another thing you have to make sure is the platform type. Make sure you set it to Any CPU. Here are my Project settings:

Make sure you sign the assembly as well!

The PNG Icon itself

The icon itself is a 16x16pixel PNG file.

Something that a lot of  people do wrong is add it via the resource configuration page (if you double click on Resource.resx). You do not need to do this, you can simply add the file to a folder called resources and change the properties of the file to ‘Embedded resource’.


K2 has multiple designers and for that reason requires the assembly to be placed in different locations. In general, deployment is as follows:

  • K2 Server – Copy the assembly to the Host Server\bin folder of your K2 installation.
  • K2 Designer for SharePoint – Copy to the GAC of the SharePoint WFE. The image should work if you copy the PNG to “12(or 14)-hive\TEMPLATE\LAYOUTS\WebDesigner\Images\Functions”
  • K2 Studio/K2 designer for SharePoint – Copy the assembly to the “Bin\Functions” of the K2 installation directory.


More info?

Together with this info and Johnny’s blogpost, plus the k2 manual, you should now be able to create your perfect inline function! If you still have questions, feel free to comment!