To be honest, i've never done it this way, i've always created SmartObjects to store my form values.
If you create a SmartObject representation of your form, for example if your form contained
Name, Address and Phone Number, create a SmartObject using a SmartObject project that contained
soId : AutoNumber
name : Text
address : Memo
phoneNum : Text
Get the wizard to create the 'create', 'load', 'save' and 'get list' methods for you.
Now, on your infopath form, perform the SmartObject integration, you can do this by right clicking on your form (if not already integrated with your WorkFlow) or in the InfoPath integration wizard. Make sure you bind each method (create, save and get list) to your form
Before you display the form to your users, use the SmartObject wizard to set some values using the create method (or the save method to update values). Store the autonumber in your workflow by assigning it
On the load event, invoke the load method of your SmartObject using a hidden field populated with the SmartObject ID
See code below:
smartObjectID = <whatever value you need from either K2 or previous forms>
// we need to get the xml from the K2 state data for my form2
// Note: 'form2Name' is the name of my infopath form as it appears in the K2 Object Browser
string k2xmlString = K2.ProcessInstance.XmlFieldsi"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 calledn '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.XmlFieldsi"form2Name"].Value = xmlDoc.OuterXml;
in form2 you can now use the hidden field to gain access to the correct smartobject.
I'm not sure if you'll hit any issues if the form has not been opened before you do all this.
but the approach above is different to the way you did it earlier, so give it a go.
Martin
I must admit I don't know how it works with SourceCode.Workflow.Common.Utilities.XMLFieldMod.SetXMLValue but there is a simple method which is proven to work if you're used to Xml / .Net development:
Just create an XML document, load your XML structure in it, do whatever you want in this doc (select nodes, assign values, etc.) then copy the content of your XML document in your K2 XML field, and you're done!
Here's a quick example:
XmlDocument
xmlDoc = new XmlDocument();
xmlDoc.LoadXml(K2.ProcessInstance.XmlFields[
"MyForm"].Value);
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
namespaceManager.AddNamespace(
"my", xmlDoc.DocumentElement.GetNamespaceOfPrefix("my"));
xmlDoc.SelectSingleNode(
"my:myForm/my:myGroup/my:myField", namespaceManager).InnerText = "HelloWorld!";
K2.ProcessInstance.XmlFields[
"MyForm"].Value = xmlDoc.OuterXml;
Thanks mgallen, your solution works fine. But I like to avoid use smartobjets, because I don't need persist data in DB.
Nicolas your solution works too, and is something like that i was thinking. You solved my problem. Thanks.
Great post mgallen, but this part:
xmlDoc.SelectSingleNode("//my:Form2Fields/my:hiddenSmartObjectID", nsMgr).Value
doesn't work for me. This one does:
xmlDoc.SelectSingleNode("//my:Form2Fields/my:hiddenSmartObjectID", nsMgr).InnerText
You can also do it with the following.
K2.ProcessInstance.XmlFieldss"MOF V3"].Value = SourceCode.Workflow.Common.Utilities.XMLFieldMod.SetXMLValue(K2.ProcessInstance.XmlFieldss"MOF V3"].Value, "/my:ChangeOrder/my:RequestDetails/@my:RequestNumber", K2.ProcessInstance.ID.ToString());