0

I have a doubt regarding setting up a value provider for prefilling Sitecore forms, I can see there are examples for simple text input fields, and it's working, but when I try to set up a value provider for a dropdown list field it's not working. I tried below but no luck, can someone help me to figure out what am doing wrong here.

public class CountryListValueProvider : IFieldValueProvider
    {
        public FieldValueProviderContext ValueProviderContext { get; set; }
        public object GetValue(string parameters)
        {
            var countries = GetCountries();
            var countrList = new ListFieldItemCollection();
            foreach (var country in countries.data)
            {
                countrList.Add(new ListFieldItem
                {
                    Text = country.code,
                    Value = country.value
                });
            }
            switch (parameters)
            {
                case "countrList":
                    return countrList;
                default:
                    return string.Empty;
            }
        }
    }

Or do I need to try something else, like using IListDataSourceProvider or something (edited)

1
  • Are you able to debug this? And check if it is creating the perfect FieldItemCollection? @NijasHameed Commented May 10, 2022 at 4:44

1 Answer 1

0

The main use of Value Provider is for prefilling Sitecore forms. So Sitecore has described how to implement that using the below article.

https://doc.sitecore.com/xp/en/developers/93/sitecore-experience-manager/walkthrough--setting-up-a-value-provider-for-prefilling-forms.html

But in your case, if you want to pre-fill your value for a dropdown list then you only need to pass a list of strings like this. For example, if the value you want pre-fill is called "prefillvalue", then you code will be like this.

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

Read this article for more details.

http://onelittlespark.bartverdonck.be/value-provider-on-list-components/

4
  • What if I wanted to pass the option containing text and value? can I use keyvalue pair? Commented May 10, 2022 at 7:16
  • 1
    @NijasHameed Follow this article if you want to achieve something that you want. (Check the title Create a List Data Source Provider) joaoneto.blog/… Commented May 10, 2022 at 7:29
  • Yes I tried this and got it worked, Thanks Commented May 13, 2022 at 5:58
  • @NijasHameed Please mark this as a verified answer if it resolved your issue. Thanks Commented May 13, 2022 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.