How to set Destination Set programmatically from the code in Workflow?


Badge +3

Hi All,

 

I have to manage destination users for client event programmatically base on a bussines logic in my application. So, using a group or individual users are not a option because I have to create hundreds of groups to cover all bussines scenarios. Instead I want to create Destination Set programmatically from Workflow itself.  I have BLL that will create a list of users for the particular Workflow Instance. Is there an example how to create Activity Destination Set from workflow code?

 

Regards,

  Oleg


14 replies

Badge +8

If you can construct a simple xml document, you can save it to a process XML field and use that as your destination set.  You can use this as a guide.

Badge +3

That is one option but I need to implement more flexible solution. I want to load users list for the destination from external source. Is there any way to add my custom code to Workflow and add users dynamically from the code when Workflo will go to the Activity with Client Event? The users list will change base on multiple conditions.

 

Regards,

  Oleg

 

Badge +8

Stick all your code in a DLL and reference this DLL in your K2 process.  Use a Server Code event to call the code in your custom assembly and provide whatever level of logic you feel is appropriate.  Save the results to an XML field, then see the link above.  All done.

Badge +3

Could you please give me an example how to use XML Fields?

 

Regards,

  Oleg

Badge +8

One is provided in the link above.

 

To save data to an XML process field within a server event, do the following:

 

K2.ProcessInstance.XmlFields["MyXmlFieldName"].Value = myXmlDoc;

 

Badge +3

Do you have any clue how XML should looks like?

 

Regards,

  Oleg

Badge +8

It can look however you want it to look.  When you provide an XSD schema for the XML data field, that tells K2 how to parse and make use of the data within the XML field.

 

For example, the link above provides the following XSD schema:

 

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="UserList">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Users" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

 To use this schema, the XML in your process field should look like the following:

 

<UserList>
<Users>domainuser1</Users>
<Users>domainuser2</Users>
<Users>domainuser3</Users>
...
</UserList>

 We use this technique every day to dynamically assign tasks to any number of users throughout our organization.

Badge +3

I am getting the error: "ActivityInstanceDestination property not available on given context." I am trying to build XML in Server Code Event.

 

        public void Main(Project_39c84fe922cb4c0da4571e96c08ec7dd.EventItemContext_5c04dde6cfb747b49bf1cf7f5b58f922 K2)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(K2.ProcessInstance.XmlFields["Reviewers"].Value);
var root = xmlDoc.CreateElement("UserList");
xmlDoc.AppendChild(root);

var userNode = xmlDoc.CreateElement("Users");
userNode.InnerText = @"K2:DOMAINuser1";
root.AppendChild(userNode);

var userNode2 = xmlDoc.CreateElement("Users");
userNode2.InnerText = @"K2:DOMAINuser2";
root.AppendChild(userNode2);

K2.ProcessInstance.XmlFields["Reviewers"].Value = xmlDoc.OuterXml;
}

 

Badge +8

When/where are you getting this error message?

 

Have you verified that the contents of the XML field is correct?

Badge +3

To test a concept I created completely new Workflow and configured XML  Field with pre-defined data (no custom code). I got exactly the same error. So, my understending is something is wrong with XML formatting. I have attached screenshots. I replaced real domain and user name by dummy values on screenshot. In real test I am using real domain and user.

Could you please share your XML/XSD?

 

Regards,

  Oleg


13543i39A258654CFB50B0.png
15792i207D9ECFE23702AD.png
Badge +8

Did you save the XML schema to a file and add it to the XML field properties?

 

What is in the XML Schema tab of your XML field properties?

Badge +3

Yes. I created XSD file and I loaded XSD from XML Schema tab. But it shows TargetNamespace:[empty].

 

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="UserList">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Users" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

 Regards,

   Oleg


16660i1CFA71A477495886.png
Badge +8

Based on the posted screenshot, you appear to be using an activity XML field, not a process XML field.  Is this correct?

 

If so, you need to use a process XML field.  Activity fields get recreated for each destination slot.  You are trying to use this field to create desination slots, but the field isn't created until destination slots are created.

Badge +3

Thank you. It is working with Instance XML Fields.

 

Regards,

  Oleg

Reply