Example: How to Work with K2.net XML Fields

baasopchoppySecurity

Nov 5, 2013 (3 years and 9 months ago)

1,435 views

Example: How to Work with K2
.net XML
Fields


1

of
4


Overview

The purpose of this document is to show how to interact with K2.net XML fields within K2.net
Studio and .NET application code.


Details


1.

In K2 Studio, make sure the XML Data Field has meta data set


K2 XML fields do not have default values, so wh
en the process instance is started, the actual
value of the string is empty (“” in C#).

However, the Meta Data property of the XML field can be
used.


To generate the Meta Data for the XML field in K2:


-

Open Process Properties

-

Select XML field in left pan
e


-

Double click the desired XML field

-

Select the “XML Metadata” tab

Example: How to Work with K2
.net XML
Fields


2

of
4


-

Click the “Create XML Instance” button

o

This should insert the empty XML doc in the field

-

Click OK to save

-

Click OK to Save


2.

Set the values in the ASP.NET plan page (aka “Start Page” or
the page that will kick off
the K2 process)

/*


Below is the XML schema for this sample. It is stored in the MetaData property


Of the K2 XML data field



<Root><Employee><FirstName /><LastName /></Employee></Root>


*/



// create a new K2ROM object

Conne
ction oConn = new Connection();


// connect to the K2 server

oConn.Open("k2vpc");


// create a new process instance

SourceCode.K2ROM.ProcessInstance oProcInst = oConn.CreateProcessInstance(@"Test
\
Simple
Test Proc");


// since this is a new process instance
, there is no default value in the XML field.
However

// we can grab the empty XML instance from the meta data field to use to

string strXml = oProcInst.XmlFields["TestXMLField"].MetaData;


// create a new .NET xml doc

XmlDocument oDoc = new XmlDocument(
);


// load up the XML doc with the empty instance retrieved from the MetaData property of
the

Example: How to Work with K2
.net XML
Fields


3

of
4

// K2 XML field

oDoc.LoadXml(strXml);


XmlNode oNd = null;


// get and set the First Name element

oNd = oDoc.SelectSingleNode("Root/Employee/FirstName");

oNd.Inn
erText = "Joe";


// get and set the Last Name element

oNd = oDoc.SelectSingleNode("Root/Employee/LastName");

oNd.InnerText = "Smith";


// set the K2 XML field from the XML document object

oProcInst.XmlFields["TestXMLField"].Value = oDoc.OuterXml;


// star
t this process instance

oConn.StartProcessInstance(oProcInst);


// close the K2ROM connection

oConn.Close();



3.

Set values from a server event within a K2 Activity



public void Main(ServerEventContext K2)

{


K2.Synchronous = true;



System.Xml.XmlNode oNd

= null;



// create a new XML doc


System.Xml.XmlDocument oDoc = new System.Xml.XmlDocument();



// load up the XML doc with the XML Field


oDoc.LoadXml(K2.ProcessInstance.XmlFields["TestXMLField"].Value.ToString());



// set the first name


oNd = oDoc.Se
lectSingleNode("Root/Employee/FirstName");


oNd.InnerText = "Jane";



// set the last name


oNd = oDoc.SelectSingleNode("Root/Employee/LastName");


oNd.InnerText = "Doe";



// set the K2 XML field


K2.ProcessInstance.XmlFields["TestXMLField"].Value = oDoc.
OuterXml;


}




Screen shot of the sample activity:


Example: How to Work with K2
.net XML
Fields


4

of
4