Some of this doesn't pertain to you but it will get you what you need to get started.
public PollResult GetPollResults(XFormControl xFormControl, XForm form)
{
IList<XFormData> data = form.GetPostedData();
PollResult result = new PollResult();
ControlCollection controls = xFormControl.Controls;
result.TotalItems = form.GetPostedDataCount();
foreach (var item in controls)
{
if (item is Select1)
{
var select = item as Select1;
if (string.IsNullOrEmpty(result.Question))
result.Question = select.Label;
var options = select.Choices.Items;
if (options.Count > 0)
{
foreach (var option in options)
{
var value = data.Where(p => p.Data.InnerText == option.Value).Count();
var resultItem = new Result();
resultItem.Name = option.Value;
resultItem.Value = value;
resultItem.Percentage = Math.Round(((double)value / (double)result.TotalItems) * 100, 1);
result.Results.Add(resultItem);
}
}
break;
}
}
return result;
}
Ended up wrtiting an extension method instead. Here is hiw we use it. XFormControl is a small class to hold the Name of the control, Lable, and Value.
var xFormControls = xFormPostedData.XFormControls().ToList();
public class XFormControl
{
///<summary>
/// Unique identifier of the control
///</summary>
public string Name { get; set; }
///<summary>
/// Label on the XForm control
///</summary>
public string Label { get; set; }
///<summary>
/// Value of the control
///</summary>
public string Value { get; set; }
}
///<summary>
/// Extension methods for EPiServer XFormPostedData
///</summary>
public static class XFormPostedDataExtensions
{
///<summary>
///
///</summary>
///<param name="xFormPostedData"></param>
///<returns></returns>
publicstaticList<XFormControl> XFormControls(thisXFormPostedData xFormPostedData)
{
XFormControl control = newXFormControl();
List<XFormControl> controls = newList<XFormControl>();
IEnumerable<XFormsFragment> xFormFragments = xFormPostedData.XFormFragments.ToList();
foreach (var fragment in xFormFragments.Where(f => f isXFormsFragment))
{
string fragValue = fragment.Value;
string fragNameX = fragment.Name;
string fragName = fragment.Reference; //
Type type = fragment.GetType();
//Template for every heading field
if (type == typeof(TextFragment))
{
control = newXFormControl();
control.Name = fragName;
control.Value = fragValue;
controls.Add(control);
}
//Template for every textbox field
if (type == typeof(InputFragment))
{
control = newXFormControl();
control.Name = fragName;
control.Label = ((InputFragment)(fragment)).Label;
control.Value = fragValue;
controls.Add(control);
}
//Template for every textarea field
elseif (type == typeof(TextareaFragment))
{
control = newXFormControl();
control.Name = fragName;
control.Label = ((TextareaFragment)(fragment)).Label;
control.Value = fragValue;
controls.Add(control);
}
//An option in a select control
elseif (type == typeof(SelectOption))
{
control = newXFormControl();
control.Name = fragName;
control.Value = fragValue;
controls.Add(control);
}
//Template for every checkbox field
elseif (type == typeof(SelectFragment))
{
control = newXFormControl();
control.Name = fragName;
control.Label = ((SelectFragment)(fragment)).Label;
List<SelectOption> options = ((SelectFragment)(fragment)).Options.Where(x => x.SelectedItem).ToList();
if (options != null)
{
foreach (SelectOption option in options)
{
if (string.IsNullOrEmpty(control.Value))
{
control.Value = option.Text;
}
else
{
control.Value += ", " + option.Text;
}
}
}
controls.Add(control);
}
//Template for every dropdown field
elseif (type == typeof(Select1Fragment))
{
control = newXFormControl();
control.Name = fragName;
control.Label = ((Select1Fragment)(fragment)).Label;
SelectOption option = ((Select1Fragment)(fragment)).Options.Where(x => x.SelectedItem).FirstOrDefault();
if (option != null)
{
control.Value = option.Text;
}
controls.Add(control);
}
//Template for every radio button field
elseif (type == typeof(Select1AsRadiobuttonFragment))
{
control = newXFormControl();
control.Name = fragName;
control.Label = ((Select1AsRadiobuttonFragment)(fragment)).Label;
SelectOption option = ((Select1AsRadiobuttonFragment)(fragment)).Options.Where(x => x.SelectedItem).FirstOrDefault();
if (option != null)
{
control.Value = option.Text;
}
controls.Add(control);
}
//Template for every dropdown field
elseif (type == typeof(Select1tAsDropdownListFragment))
{
control = newXFormControl();
control.Name = fragName;
control.Label = ((Select1tAsDropdownListFragment)(fragment)).Label;
control.Value = fragValue;
}
//Template for every submit button
elseif (type == typeof(SubmitFragment))
{
//This is a submit button
}
}
return controls;
}
}
I'm trying to format the xForm email. We have overriden CustomXFormPageUnknownActionHandler because the channel options are alway None when using the OnbeforeSubmit event.
I have tried getting the form data with xFormData.GetValues(). This only returns the field names and user entered data. How can I get the lable values you see on the form? I was able to iterate through the form fragments with limited success. InputFragment work but when it comes to Checkboxes or Radio button, I can see the lables. The fragments only GetValues() and fragments only return 1 or 0 for radio buttons and not the text.