I was asked recently how one can work with arrays in K2. The specific scenario presented to me was where one had a SharePoint delimited values such as “7#;Acme Bank” into the individual elements.
This can be very easily handled with K2 Inline Functions.
FYI, you can use Inline Functions in nearly any wizard; they are accessed via the context browser here:
Ok, so how would we use Inline Functions to emulate the scenario below? Attached is a simple self-contained example for your reference, but the highlights of it are:
- I created 3 process datafields
- FullString – this simulates your SharePoint xml node, it contains a default value of: 7#;Acme Bank
- Drag out a Data Event onto the Design Canvas
- Select ‘Next’ on the welcome screen
- Select ‘Transfer Data’ on the 2nd dialog
- Click ‘Assign’ button
- In the ‘Destination’ field I drag “ID” from the Context Browser process datafields into it
- I select the Inline Functions tab in the Context Browser and then expand “Lists”
- I then select the “Index Item” method and drag and drop into the “Source” field
- This will then open up a new dialog to configure the extraction of an array
- I then set the following values:
- Index: 1 (note: in K2 arrays are 1 based not 0 based)
- Expected when empty: 0
- In the ‘Values’ field I drag and drop the Inline Function Text > Split from the context browser
- This will open up a new dialog to configure a string to be split into an array
- I set the values of this Split dialog as follows:
- Text: [Process Data Field].FullString (NOTE: you would use your xml field here)
- Separator: #;
- Click Ok
- Now repeat steps 5 – 10 but substitute:
- In step 6:
- Use ‘Value” process data field
- In Step 8:
- The final Data Event should look something like:
13. Save, deploy and run. We should see the string split into individual values:
In addition to this scenario there are other usages potential usages for arrays in K2. Such as:
– if a string held a delimited list of Domain\UserNames, one could use the Split Inline Function on that field within a Destination Rule in order to leverage generate a list of Destination Users.
– If a string held a delimited list of data for which sub workflows needed to be individually invoked in by an IPC event. The Split Inline Function could be used within the Plan Per Slot – No Destination option of the Advanced Destination Rules. For more information on Plan Per Slot – No Destination, which is essentially a For-Each loop within K2, please refer to this previous post:
I know that you can use a deployment package to move environment settings to different servers, but I wanted to see if I can create them programmatically. The process of creating Environment Fields can be a time consuming and tedious task, especially if you have a large number of fields. Typically in a real-world project, a list of Fields are already defined in a functional document somewhere, so to have a utility which can create fields from a list in a text file for example could be a big time saver. At the time of this writing, documentation on how to do this kind of thing was rather scarce, hence this post 😉
The trick is to get a new instance of an Environment Field since is doesn’t have a new() constructor, you have to get a new instance by using the CreateFieldInstance() method of the EnvironmentFieldType class.
- Create a new EnvironmentSettingsManager Instance and Connect to the K2 Server using the ConnectToServer() method of the EnvironmentSettingsManager instance.
- Create a new EnvironmentTemplateCollection Instance and a new EnvironmentInstanceCollection Instance.
- Select the Environment Template to use (e.g. Default Template) and select the Environment to use (e.g. Development)
- Get a handle on the EnvironmentFieldType you want to use by calling the EnvironmentSettingsManager.EnvironmentFieldTypes.GetItemByFriendlyName(“Miscellaneous Field”)
- Get a new EnvironmentField instance by calling CreateFieldInstance() method of the EnvironmentFieldType instance.
- Set the properties of the EnvironmentField instance
- Add the Field to the EnvironmentFieldCollection of the Environment Instance with the Add(newField) method of the EnvironmentFieldCollection
- Call the SaveUpdate() method of the EnvironmentField
EnvironmentInstance ei = new EnvironmentInstance();
ei = eic.GetItemByName("Development");
EnvironmentFieldType eft = esm.EnvironmentFieldTypes.GetItemByFriendlyName("Miscellaneous Field");
EnvironmentField ef = (EnvironmentField)eft.CreateFieldInstance();
ef.FieldName = "DeonTestField";
ef.FieldDescription = "Deon Test Field";
ef.DisplayName = "DeonTestField";
ef.Value = "Some Value in Here";
efc = new EnvironmentFieldCollection(ei);
efc = ei.EnvironmentFields;