Jonas Lindau
Mar 24, 2011
  3503
(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
How to fix scheduled job 'Remove Abandoned BLOBs' if it keeps failing

Optimizely inlcudes a job named 'Remove Abandoned BLOBs'. This post will help you fix it if it's no longer working.

Henning Sjørbotten | Sep 26, 2023 | Syndicated blog

Optimizely Web Experimentation Metrics

Within the domain of Optimizely Web Experimentation Metrics, the emphasis is on objective key performance indicators (KPIs) selected to assess an...

Matthew Dunn | Sep 23, 2023 | Syndicated blog

Optimizely For you Intranet

Having been at Optimizely and in the CMS industry for nearly 16 years I have seen clients’ intranet requirements go from a simple site just to hous...

Robert Folan | Sep 22, 2023

Vulnerability in EPiServer.GoogleAnalytics v3 and v4

Introduction A potential security vulnerability was detected for Optimizely Google Analytics addon (including EPiServer.GoogleAnalytics and...

Bien Nguyen | Sep 20, 2023

Overriding Optimizely’s Content Recommendations Block to Implement Custom Recommendations

Introduction The Content Recommendations add-on for Optimizely CMS dynamically recommends content from your site tailored to the interests of each...

abritt | Sep 13, 2023 | Syndicated blog

Developer contest! Install the AI Assistant and Win Bose QC45 Headphones!

We are thrilled to announce a developer contest where you have the chance to win a pair of Bose Headphones. The goal is to be the first developer t...

Luc Gosso (MVP) | Sep 7, 2023 | Syndicated blog