How to set XML Field Value

  • 26 September 2007
  • 16 replies
  • 22 views

Badge +2

Hello,


I'm having problems with setting the XML Field values.
I have tried 2 methods and none of them worked for me, so I guess I'm doing something wrong.


Here is the case:


I created new XML Field called TestXMLField. There I have:
<TestElement>
  <TestName />
</TestElement>

Method 1. (SmartObject Event)
When I set Return mapping to XML field node <TestName> nothing happens. Value is not set and no error.
If I map same SO filed to process DataField it works so Smart Object is ok.
If I map same SO field to XML Field node that was created by Infopath Integration, Value is also not set and no error.


Method 2. (Server Event code)
            SourceCode.Workflow.Common.Utilities.XMLFieldMod.SetXMLValue(K2.ProcessInstance.XmlFields["TestXMLField"].Value,"TestElement/TestName ","Some new value");
Result is the same, Value is not set and no error.


 


So, what am I doing wrong? Can someone please help?


 


 


16 replies

Badge +2

Ok now I know what was wrong in Method 2.


It should be:
K2.ProcessInstance.XmlFields["TestXMLField"].Value = SourceCode.Workflow.Common.Utilities.XMLFieldMod.SetXMLValue(K2.ProcessInstance.XmlFields["TestXMLField"].Value,"TestElement/TestName ","Some new value");


But issue with Method 1 remains. SmartObject Event cant set value of XML Data field.


Please help

Badge +2

Is it eaven possible to map SO to XML Field or it is only possible to map to Data Field??


Danko

Badge +8

I am experiencing the same problem and it is real frustrating.


The only solution I had was to create a dummy process field, set the return value into that field and then set that back into the XML of InfoPath doc.  I bet if I just hooked up to the web service in the InfoPath form itself instead of doing this on the K2 site it would work.

Badge +8

I've tried using your "updated" method 2 and it doesn't work :-(


Do you simply use this code line in a default server event? In fact, there's no error but no value too.


My case may be quite particular : the process instance starts with a Sharepoint event then the first activity is handled through an Infopath Client Event.


 


Thanks!

Badge +8

Mine was started by an InfoPath form and then the first activity was a SmartObject event.  I just have not had the time, but I bet there is just a small bug in the generated code and if you go it, you can probably resolve it...


Jason

Badge +8

Thanks Jason. But actually, is it simply possible to set an InfoPath field value using code in a Default Server Event, without a SmartObject?


I can do it modifying the code behind the InfoPath Client Event (I've tried, and it works), but this is so dirty!

Badge +8

That is really easy.  So my work around was to use a SmartObject event and then set the return value in an activity field.  Then on the same activity where the SmartObject event is, I drag on a code event after the SmartObject event.  Then I go in and add this code.


 Hope this helps - Jason


//add this to the top
using System.Xml;


//Here is the code
string xml = K2.ProcessInstance.XmlFields[0].Value.ToString();


XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);


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


doc.SelectSingleNode("//my:myFields/my:Field1", nsMgr).InnerText = K2.ActivityInstanceDestination.Fields["TempField"].Value;


K2.ProcessInstance.XmlFields[0].Value = doc.OuterXml;

Badge +8

Thanks Jason; actually, it confirms what I thought, as this is exactly what I did.


The problem is that I don't start the process with InfoPath: this means that the form structure is not instanciated before reaching the InfoPath Client Event step (I've just figured this out). Then K2.ProcessInstance.XmlFields[0].Value is empty and I can't do anything.


Conclusion: when a process is not started using InfoPath, you cannot set the value of the form fields using Blackpearl. For example, in my case, a process instance is run when a user add an item to a document library. Then a manager should have a workitem corresponding to an InfoPath form with its fields filled in with the properties of the added document (title, URL, size, etc.), so that they can validate it (I cannot use MS Office embedded workflow capacity as it might be any sort of document).


I'm stuck :-(

Badge +8

Did you know you can call a SmartObject method from InfoPath directly?  It works really well...


Anyways, there is no reason why you cannot take a string of XML and set it into the XML field.


I have to say you conclusion is a little incorrect.  I have successfully processes in the past that start with a custom asp page and then use infopath in the same process.


All you need to do is set the XML yourself.

Badge +8
icon-quote.gifjapergis:

Anyways, there is no reason why you cannot take a string of XML and set it into the XML field.


[...]


All you need to do is set the XML yourself.



 


This is completely true. But prior to this you need to get all the structure, which is not available. I could only find it in the Metadata property of the XmlField, but the deployment path and the version token are replaced by variables by Blackpearl. And I haven't found them anywhere.


 


Concerning the use of a SmartObject, I know this solution works in our case as this is the one we use but I'm still disappointed that the simple solution which consists in passing a value to a specific field isn't possible (whereas I understand why).


 


Anyway, a SmartObject is fine if this is the only way to do this.

Badge +8

I am not sure what you mean by "But prior to this you need to get all the structure, which is not available"


Are you saying you cannot get to the XSD of the IP form so that in your ASP you cannot create the well formed xml for the infopath form?

Badge +8
Yeah Jason, exactly... But I must admit I haven't investigated it anymore (I was fully satisfied with the use of a SmartObject doing what I wanted) and I've heard it's been solved with SP1. As far as I remember, it was like if the XML schema was only available after the first client event (since the process didn't start with Infopath).
Badge +8

When you say available you mean it just has not been populated yet - it was null.  I believe it is correct that it remain null until you set a value to it :-) 


I guess you are not being completely clear but once an infopath form is pulled in, it is added as a process level field so it is absolutely available before the first client event.  All you need to do is set the xml field with a string of xml. 


This does not apply to your situation but here is a little tip.  For example this weekend I was playing around.  I had two processes, one an infopath and the second a sharepoint integrated process.  The sharepoint integrated process needed to use the XML created from the infopath process.  The processes are not chained together through an ipc event; a user must in manually start each of them but the xml created in the infopath process will be used in the sharepoint integrated process.  I went to design mode in the infopath form.  Then did a Save as Source Files.  There should be an .xsd called "main" if my memory serves me right.  I then created a new xml field using the xsd file.  I was able to subsequently load in the xml string into the sharepoint integrated process.  In this example I was able to use the xml from an infopath form and did not use any of the infopath integration wizards or events.


 

Badge +8

No no, I'm not - or at least wasn't - talking about the data, but the structure itself. I know the Infopath XML fields are added to the Blackpearl XML Fields but before I got through the first client event, it was exactly like if this structure didn't exist. Just to give a fake example (as I don't remember the exact error), if I wanted to do something like myXml.selectSingleNode("rootNode/node1").Value = "blahblah"; I got something like 'rootNode/node1 doesn't exist'. And if I copied/pasted this code just after my client event, it worked.


However, I don't exclude this is "local" bug due to my environment, as this seems to work for you and other guys here (and maybe for me, I'll have to try now that we have SP1) :-)

Badge +8

Yep - that would the exact error you should get in my humble opinion.  However I have used the xml fields to store complex data structures, not for infopath processing, so making sure I do not reference something that could be null is part of my design.


If K2 did something to pre-populate the "infopath" xml field prior to the first infopath client event, that is ok too.

Badge +8

Just to give a last update about this, I had to design another workflow where I had to set an Infopath field value before the first client event, I took the opportunity to test the "direct" method (i.e. assigning a value to the XML field in K2) and it works! This was the first time I tried since SP1 has been released.


 I know it worked for you Jason before the SP1 was released but I can assure you I really had a problem with that with Blackpearl HF 2.01 :-)

Reply