xmlDataFields instead of datafields

  •  10-23-2008, 1:47 PM

    • Ericx is not online. Last active: 12-23-2008, 10:45 Ericx
    • Top 500 Contributor
    • Joined on 08-22-2008
    • Virginia Beach, VA
    • Posts 12
    • Points 48

    xmlDataFields instead of datafields

    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 = "My\\HelloWorld";
    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.XmlFields["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.XmlFields["employees"].value = wut.OuterXml;
    cpi.update();
    clientConnection.close();

View Complete Thread