Value Provider on list components

Since version 9.1, Sitecore Forms supports prefilling by using value providers. (The functionality is also available for version 9.0 via the Forms Extensions module)

Out of the box, Sitecore only provides the Value Provider system, but doesn’t have any implementation. When you install SFE, 24 value providers linking to the xDB profile will be available at your disposable.

If you want to create your own value providers, have a look at the official documentation. https://doc.sitecore.com/developers/93/sitecore-experience-manager/en/walkthrough–setting-up-a-value-provider-for-prefilling-forms.html

Creating a value provider comes down to creating a class implementing the IFieldValueProvider interface and registering this class in Sitecore. This interface has 1 method public object GetValue(string parameters).

These value providers work great on components like a textbox, an email fiels, a phone field, etc.. However, many have struggled to getting this to work with list components. (Drowdown list, List box, Checkbox list, …) Returning a string with the value to prefill or preselect makes the system crash.

The trick here is that these components expect a list of strings to be returned by the GetValue method. Even when you only have a single string value to return as a prefill (or preselect) for the list, you need to wrap this string into a list of strings.

public object GetValue(string parameters)
{
   return new List<string> {"prefillvalue"};
}

This method makes it possible to have multiple items preselected on your list component.

Leave a Reply