Jonas Lindau
Mar 24, 2011
  3655
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024