How to parse XML for email notification?


Badge +1
We have an XML data field in K2 studio. If I insert that field into an email event, then it only displays one of the XML values, not all of them.

How do I unwrap the XML in code in K2 studio so that all the values will display?

2 replies

Badge +4
Here is some example code in Set Authorisation Level Server Event that you can have a look at.

The following code will set the value of ManagerDisplayNextAuthSelection based on the value of PurchaseTotal.


Sub Main(ByVal K2 As ServerEventContext)
K2.Synchronous = True

Dim PurchaseTotal As double= CDbl(SourceCode.K2Utilities.XMLFieldMod.GetXMLValue(K2.ProcessInstance.XmlFields("K2InfoPathSchema").Value,"my:PurchaseOrder/my:Order/my:OrderPriceTotal"))

If PurchaseTotal > CDbl(K2.StringTable("Level2ApprovalLimit")) Then
K2.ProcessInstance.XmlFields("K2InfoPathSchema").Value = SourceCode.K2Utilities.XMLFieldMod.SetXMLValue(K2.ProcessInstance.XmlFields("K2InfoPathSchema").Value,"my:PurchaseOrder/my:Manager/my:ManagerDisplayNextAuthSelection","True")
Else
K2.ProcessInstance.XmlFields("K2InfoPathSchema").Value = SourceCode.K2Utilities.XMLFieldMod.SetXMLValue(K2.ProcessInstance.XmlFields("K2InfoPathSchema").Value,"my:PurchaseOrder/my:Manager/my:ManagerDisplayNextAuthSelection","False")
End If

End Sub


Example code in Timestamp Approval Server Event
The following code will set a timestamp into ManagerApprovalDateTime.


Sub Main(ByVal K2 As ServerEventContext)
K2.Synchronous = True

Dim sTimeStamp As String = Now.ToString("MM/dd/yyyy HH:mm:ss tt")
K2.ActivityInstanceDestination.XmlFields("K2InfoPathSchema").Value = SourceCode.K2Utilities.XMLFieldMod.SetXMLValue(K2.ActivityInstanceDestination.XmlFields("K2InfoPathSchema").Value,"my:PurchaseOrder/my:Manager/my:ManagerApprovalDateTime",sTimeStamp)

End Sub
Badge +8
You can use the SourceCode.K2Utilities namespace to load an XML element and loop through the nodes in the element and add each node's value to your email - this code snippet should get you started:

.Value, "my:myXMLRoot/my:EmailElements"); 
for (int i = 0; i < xmlNodeList.Count; i++)
{
//do something with the value of the element, maybe append it to a string
emailText += xmlNodeList.Item(i).InnerXml;
}

Reply