Monday, November 23, 2009

Add InfoPath fields to Notification Email

In one of my K2 project which is an InfoPath Integrated Workflow, the users would like to see some information in InfoPath form, such as Company Name, Region, Office and so on in the notification email, so they can easily figure out what this email is about.

Well, this is a very good suggestion. Unfortunately in Notification Email Design Wizard in K2 workspace, there’s no way to get XML fields in the InfoPath form. After some investigations, I found Data Fields show in the Notification Email Design wizard. So we can create Data Fields and assign values for these data fields from xml fields. It is pretty simply to do it. The following sample creates two data fields Office and AdminRegion. In the workflow, add a Default Server Event (code), add the following code:

// The xml string in K2
string xml = K2.ProcessInstance.XmlFields["XML root field name"].Value;
// Load xml string to a xml object
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNamespaceManager nsgr = new XmlNamespaceManager(doc.NameTable);
nsgr.AddNamespace("my",doc.DocumentElement.GetNamespaceOfPrefix("my"));

//Get Office value
string office = doc.SelectSingleNode("//my:Office",nsgr).InnerText;

//Get AdminRegion value
string adminRegion = doc.SelectSingleNode("//my:AdminRegion", nsgr).InnerText;

K2.ProcessInstance.DataFields["Office"].Value = office;
K2.ProcessInstance.DataFields["AdminRegion"].Value = adminRegion;



Now we can use Office and AdminRegion which are populated from the form in Notification email.

No comments:

Post a Comment