Try our conversational search powered by Generative AI!

Jonas Lindau
Mar 24, 2011
  3623
(0 votes)

XForms: Collecting data and labels and pass it to an existing webservice

Objective

Let service desk staff create forms containing support question using XForms and post it to an existing webservice. They should be able to create as many forms as they needed, with different questions.

 

So it begins…

I really hadn’t done much work with XForms before, just some templates similar to the demo template. I knew there ware an option to post forms to a specified URL, so i thought this would be really easy. Just build some forms, set it to post to an URL and we should be fine.

Well, it wasn’t that easy… i found out that the option “Post to specified URL” really does nothing unless you add the code for it yourself. Fair enough, it should be easy.

Adding some code to the BeforeSubmitPostData and AfterSubmitPostData, collect all data and post it to the URL stored in e.FormData.CustomUrl. should solve this. Or?

Well, i could be enough most times, but i needed to get my hands on the text inside the labels. The webservice wanted me to send in all text in one string, something like this:

Label 1\n
Value inside the textbox\n
\n
Label 2\n
Value inside the textbox\n

Getting the labels proved to be the main issue here. Is far as i know, the labels are nowhere to be found within the XForm-classes. So, what to do?

I turned out i had to parse the forms XML to get my hands on the labels. This is what i added to BeforeSubmitPostedData to get all textfields:

protected void XForm_BeforeSubmitPostedData(object sender, SaveFormDataEventArgs e)
{
    XFormControl control = (XFormControl)sender;

    XmlDocument Doc = new XmlDocument();
    Doc.LoadXml(control.FormDefinition.Document.InnerXml);

    XmlNamespaceManager ns = new XmlNamespaceManager(Doc.NameTable);
    ns.AddNamespace("xforms", "
http://www.w3.org/2002/xforms");

    XmlNodeList InputFields = Doc.SelectNodes("//xforms:input", ns);

    foreach (XmlNode thisNode in InputFields)
    {

//Inside this loop, you can access the label by using
//thisNode.InnerText
//
//The value of textfield can be accessed by using:
//e.FormData.GetValue(thisNode.Attributes["ref"].Value)

   }
}

So i just stored all label values and textfield values inside an array of KeyValuePair, used a stringbuilder to put it all together into a string with the expected format and passed it on to the webservice.

It wasn’t quite as easy as i thought, but in the end it wasn’t really that hard either.

Mar 24, 2011

Comments

Mar 24, 2011 04:49 PM

It's never hard when you know what to do ;)
Nice work though, always fun to work with a XML document!

Mar 25, 2011 12:22 PM

Nice to see that you solved this. If someone else needs to solve this I have some additional information:

1. If you are using labels on the XFormControls (and not loose text nodes), which you should for accessibility reasons, you should be able to access the Label property on XFormControlBase which all XForm controls inherit from. This way you should be able to use the ExtractXFormControls method to extract the form controls and get the label and value from them.

2. "Post to specific URL" should start up a WebClient class and use the UploadValues method to the specified URL. But these values does not contain the labels for the form field, just the data name and value.

Please login to comment.
Latest blogs
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog