Setting an InfoPath field value outside of InfoPath in a K2 [blackpearl] workflow

The following code shows how to set an XML field on an InfoPath form in a server code event. The example uses ’smartObjectID’ as the variable that I am using as the input to the InfoPath field. ‘form2Name’ is the name of the InfoPath form as it appears in the K2 Object Browser in Visual Studio. ‘hiddenSmartObjectID’ is the name of the text box control that I’ve placed on my InfoPath form. Once the code has completed execution, the form form will load with the value already set allowing the utilisation of it at a later time in the workflow.

This is useful when you are providing more than one form to the user via Infopath client events, and the data needs to be linked in some way.

smartObjectID =
    <whatever value you need from either K2 or previous forms> 

// we need to get the xml from the K2 state datastring k2xmlString =
    K2.ProcessInstance.XmlFields["form2Name"]    .Value.ToString();

// get stuff from form2
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(k2xmlString);

// create a namespace manager for InfoPath
XmlNamespaceManager nsMgr =
    new XmlNamespaceManager(xmlDoc.NameTable);
nsMgr.AddNamespace("my",
    xmlDoc.DocumentElement.GetNamespaceOfPrefix("my"));

// set the value of a hidden field to be equal to the id 
// required for retrieving the correct smartobject
// NOTE: I've got a hidden text box on my infopath form
// called 'hiddenSmartObjectID' that I'll be using later
xmlDoc.SelectSingleNode
    ("//my:Form2Fields/my:hiddenSmartObjectID", nsMgr).Value =
          smartObjectID;

//Now set the value back into the XML Field
K2.ProcessInstance.XmlFields["form2Name"].Value =
     xmlDoc.OuterXml;

Let me know how you get on with this. :-D

SpittingCAML




You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply