Wednesday, June 25, 2014

Reading Complex Field Values in Sitecore WFFM

Sitecore Web Forms for Marketers is a Sitecore CMS module used for creating forms in a website. It has various set of fields like simple, list, complex, pre-filled etc.

Usually to access field value in code, Value property of AdaptedControlResult class is used.
For any list or complex types of fields, the Value property normally contains extra information.

For e.g., A WFFM form has a check-box list and the values for its items are 1,2,3,4,5. User selects 1,2,3 and submits the form.
Then in the server-side code the value of this field is:

<item>1</item><item>2</item><item>3</item>

To get a simple value with out item tags then GetSitecoreStyleValue method of FieldReflectionUtil class can be used.
Example code snippet:

AdaptedResultList fields;

// Get the form field by specifying the field name
AdaptedControlResult formField = fields.GetEntryByName("fieldName"); 

// complex value with item tags
string fieldValue = formField.Value;

// simple value without item tags
fieldValue = Sitecore.Form.Core.Utility.FieldReflectionUtil.GetSitecoreStyleValue(formField.FieldID, fieldValue);


BottomLine: Use FieldReflectionUtil class to get simplified version of form field values.