Skip to main content

I have a workflow that has a form where datafields would be hard to manage (80 of them) and the data is not really normalized for a smart object. Instead we used xmldatafields.


I don't have time to pretty this up but wanted to post my notes.  I hope they help you.


Things to know, use client connection not management connection.



  •  SourceCode.Workflow.Client.ProcessInstance npi = this.clientConnection.CreateProcessInstance(K2processName);

When you get an XmlDataField it comes out as a string, you need to make it an xmldocument or reader/write (your choice).


After you make the string an XmlDocument , get the root node, then do your work on it.


When you write it back to the process, it's not a string as it came to you, but an XmlDocument.


UPDATE YOUR PROCESS! or when you close your connection your changes will be gone.


----Code not tested in this format, I had a bunch of classes so some pieces may be missing.


SourceCode.Workflow.Client.

Connection clientConnection = new SourceCode.Workflow.Client.Connection();
clientConnection.Open(_serverName);
String K2processName = "MyHelloWorld";
SourceCode.Workflow.Client.ProcessInstance npi = tclientConnection.CreateProcessInstance(K2processName);
npi.Folio = "Development Test";
clientConnection.StartProcessInstance(npi, true);
Int new_process_id = npi.ID;
npi.update();
clientconnection.Close(); //Just to show you that you should close it.


XmlDocument wut = new XmlDocument();
XmlElement root;


SourceCode.Workflow.Client.Connection clientConnection = new SourceCode.Workflow.Client.Connection();
clientConnection.Open(_serverName);
SourceCode.Workflow.Client.ProcessInstance cpi = clientConnection.OpenProcessInstance(new_process_id);
String xml_as_string = cpi.XmlFieldsl"WU"].Value.ToString();
wut.LoadXml(xml_as_string);
root = this.wut.DocumentElement;
XmlNamespaceManager ns = new XmlNamespaceManager(wut.NameTable);
ns = ns;
ns.AddNamespace("", http://schema.example.com/employee/employee.xsd); // set default
ns.AddNamespace("wu", http://schema.example.com/employee/employee.xsd);


//There are a million different ways to get data from an xml Document, this is how I did it this time, could not get xpath expressions to work properly with selectSingleNode so  


//Get some values


String xpparent = "//wu:employee";
String xpchild = "wu:employee_name";
XmlNode parent = this.root.SelectSingleNode(xpparent, this.ns);
XmlNode child = parent.SelectSingleNode(xpchild, this.ns);
String EmployeeName =  child.InnerText.ToString();


//set the value
child.InnerText = "Charlie Brown";


String xpparent = "//wu:employee";
String xpchild = "wu:employee_id";
XmlNode parent = this.root.SelectSingleNode(xpparent, this.ns);
XmlNode child = parent.SelectSingleNode(xpchild, this.ns);
String EmployeeID = child.InnerText.ToString();


child.InnerText ="123456";


cpi.XmlFieldsm"employees"].value = wut.OuterXml;
cpi.update();
clientConnection.close();

Hi Ericx,

If I may, just one remark concerning your samples...

In a CreateProcessInstance case, instead of :

  1. OpenConnection
  2. CreateProcessInstance
  3. StartProcessInstance
  4. Update
  5. Close

it's better to use these succession :

  1. OpenConnection
  2. CreateProcessInstance
  3. DataField or XmlField Set
  4. StartProcessInstance
  5. Close

because... this require no more rights than Start Rights, and one call less to the K2 server... and you can use your XMLField or DataField directly in the first line of your design.

I hope this helps


Reply