Jonas Lindau
Mar 24, 2011
  3727
(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
CMP DAM asset sync to Optimizely Graph self service

The CMP DAM integration in CMS introduced support for querying Optimizly Graph (EPiServer.Cms.WelcomeIntegration.Graph 2.0.0) for metadata such as...

Robert Svallin | Feb 13, 2025

PageCriteriaQueryService builder with Blazor and MudBlazor

This might be a stupid idea but my new years resolution was to do / test more stuff so here goes. This razor component allows users to build and...

Per Nergård (MVP) | Feb 10, 2025

Enhancing Optimizely CMS Multi-Site Architecture with Structured Isolation

The main challenge of building an Optimizely CMS website is to think about its multi site capabilities up front. Making adjustment after the fact c...

David Drouin-Prince | Feb 9, 2025 | Syndicated blog

How to: set access right to folders

Today I stumped upon this question Solution for Handling File Upload Permissions in Episerver CMS 12, and there is a simple solution for that Using...

Quan Mai | Feb 7, 2025 | Syndicated blog