Prefill Sitecore form fields with query parameters using custom value provider

Recently we had a requirement that if some user wants to buy the same product again on site then he/she can scan the QR code provided to him/her in the first purchase. By scanning the QR code, user will be navigated to form with prefilled fields

After Sitecore 9.3 version, we have Value Providers for pre filling the data in Sitecore form fields. These can be found in Sitecore at location – /sitecore/system/Settings/Forms/Value Providers.

By default, we can prefill your Sitecore Form with contact data from the xDB profile. However, prefilling the fields from query string needs custom value providers.

Following are the steps to accomplish this:

Create custom value provider:

  1. You may create a folder ValueProviders.
  2. In that folder, you may create your custom value provider classes and inherit from the IFieldValueProvider class.
  3. Implement the GetValue method to return an object of any type. You can pass any type of parameters and then parse it to be using in custom class.

    Following is the example of code for pre filling first name from query string parameter ‘shiptoname
using Sitecore.ExperienceForms.ValueProviders;
using System.Web;

namespace Demo.Classes.ValueProvider
    /// <summary>
    /// Defines the "FirstNameQueryString" />.
    /// </summary>
    public class FirstNameQueryString : IFieldValueProvider
        public FieldValueProviderContext ValueProviderContext { get; set; }

        public object GetValue(string parameters)
            HttpContext httpContext = System.Web.HttpContext.Current;
            string fName = HttpUtility.ParseQueryString(httpContext.Request.Url.Query).Get(parameters);

            if (string.IsNullOrWhiteSpace(fName))
                return string.Empty;

            return fName;

Create corresponding value provider items and link the custom methods:

  1. In /sitecore/system/Settings/Forms,  create a separate folder for query string value providers.
  2. Create value provider items from /sitecore/templates/System/Forms/Value Provider template. Add model class details on the item in field Model Type.
  3. Save the item.

Link the value provider to Sitecore form fields:

  1. Click the Name field, and in the Form elements pane, in the Advanced settings section, in the Value provider field, select the custom provider you have created.
  2. In the Value provider parameters field, enter shiptoname.
  3. Save the form.

Similarly, any type of logic can be used Custom value provider to autofill fields based on the requirement.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s