November Happy Hour will be moved to Thursday December 5th.

Jonas Lindau
Mar 24, 2011
  3696
(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
Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog

I'm running Optimizely CMS on .NET 9!

It works 🎉

Tomas Hensrud Gulla | Nov 12, 2024 | Syndicated blog

Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog