Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.

 

Nahid
Aug 12, 2024
  439
(4 votes)

Creating Custom Feed Options for Optimizely Forms Selection Elements

Optimizely Forms is a versatile tool for building forms used in registrations, job applications, surveys, and much more. These forms can include a variety of elements like text boxes, selection lists, date pickers, and range sliders, and so on. Regarding selection elements, Optimizely provides a built-in manual choice option where you can manually insert choices, also known as feeds.

However, there are scenarios where you might want to populate these choices dynamically. This could be from a hardcoded list or via an API, rather than manually inputting each option. In this blog post, we’ll explore how to create a custom feed option for selection elements in Optimizely Forms, allowing you to dynamically generate choice items tailored to your specific needs. 

First, to create a custom feed option for a selection element in Optimizely Forms, we need to implement a feed class that adheres to the IFeed and IUEntityInEditView interfaces. This custom feed class will be responsible for dynamically generating the choice items displayed in the selection element.

In this implementation, the LoadItems method will be responsible for providing the list items. For simplicity, we will use a static list containing a set of companies. However, you can easily replace this with an API call or any other data source that suits your needs.

Here, The Id property in the feed class acts as the unique identifier for the feed, and the Description property, which you should set in the field, will appear as the display name for this feed in the Optimizely Forms UI.

To ensure that your custom feed is recognized by Optimizely Forms, you need to register it as a feed service type using the ServiceConfiguration attribute in the class definition.

[ServiceConfiguration(ServiceType = typeof(IFeed))]
public class CompanyFeed : IFeed, IUIEntityInEditView
{
    // Unique identifier for the feed
    public string ID => "13b098f3-f960-4465-9cfd-77db88486921";

    // Display name for the feed
    public string Description { get; set; } = "Company List";

    // Extra configuration options (optional)
    public string ExtraConfiguration { get; }

    // Friendly title for the edit view
    public string EditViewFriendlyTitle => Description;

    // Indicates if the feed is available in the edit view
    public bool AvailableInEditView => true;

    // Method to load the feed items
    public IEnumerable<IFeedItem> LoadItems()
    {
        var feedItems = new List<IFeedItem>();

        // Populate the feed items using the static method GetCompanyList
        foreach (var company in GetCompanyList())
        {
            feedItems.Add(new FeedItem
            {
                Key = company.Item2,
                Value = company.Item1
            });
        }

        return feedItems;
    }

    // Static method to get the company list
    public static List<Tuple<string, string>> GetCompanyList()
    {
        return new List<Tuple<string, string>>
            {
                new("1", "Company A"),
                new("2", "Company B"),
                new("3", "Company C"),
            };
    }
}

Next, we'll create a feed provider to manage and retrieve the available feeds. The FormFeedProvider class will be responsible for retrieving all instances of the IFeed interface. This allows Optimizely Forms to recognize and utilize any registered feeds.

public class FormFeedProvider : IFeedProvider
{
    public IEnumerable<IFeed> GetFeeds() => ServiceLocator.Current.GetAllInstances<IFeed>();
}

Now that our custom feed options are ready, you can use them in the Optimizely Forms admin UI. Follow these steps:

  1. Go to the Optimizely admin UI.
  2. Create a new form or edit an existing one.
  3. Add a selection element to the form.
  4. In the choice items configuration for the selection element, you will see the custom feed option available.

By following these steps, you will be able to dynamically populate choice items in the selection element using the custom feed.

Aug 12, 2024

Comments

Brian Miller
Brian Miller Aug 20, 2024 03:16 PM

This is incredibly helpful Nahid.  I'm working through a use case where we would want to create several of these IFeeds in a way that a content editor could create new ones and or modify existing ones and they would be available across all forms.  I'd be interested in your thoughts around this as it seems like the perfect extention to what you've already created.

Nahid
Nahid Aug 27, 2024 12:41 PM

Thanks Brian. Let me know if you face any difficulties 

Brian Miller
Brian Miller Aug 27, 2024 01:14 PM

Oddly enough the 1st hurdle is that this is set up as a multi-site instance and we're initially havong difficulties registering these IFeeds to only be usable on the individual sites.  It seems as if these are an all or nothing kind of thing.  Any thoughts?

Nahid
Nahid Sep 2, 2024 01:53 PM

In general, it's not.
However, you can make custom code and implement logic based on your requirements. 
Use this AvailableInEditView to show/hide feed based on your logic 

    public bool AvailableInEditView => IsSiteHasFeed();  
    private bool IsSiteHasFeed()
      {
          var siteDefinitionRepository = ServiceLocator.Current.GetInstance<ISiteDefinitionRepository>(); // it might not need
          var definitions = siteDefinitionRepository.List(); // to list your site.
          var currentDefinition = SiteDefinition.Current; // Check your site is matched or not
          //implement logic 
          return true;
      }

Please login to comment.
Latest blogs
Level Up with Optimizely's Newly Relaunched Certifications!

We're thrilled to announce the relaunch of our Optimizely Certifications—designed to help partners, customers, and developers redefine what it mean...

Satata Satez | Jan 14, 2025

Introducing AI Assistance for DBLocalizationProvider

The LocalizationProvider for Optimizely has long been a powerful tool for enhancing the localization capabilities of Optimizely CMS. Designed to ma...

Luc Gosso (MVP) | Jan 14, 2025 | Syndicated blog

Order tabs with drag and drop - Blazor

I have started to play around a little with Blazor and the best way to learn is to reimplement some old stuff for CMS12. So I took a look at my old...

Per Nergård | Jan 14, 2025

Product Recommendations - Common Pitfalls

With the added freedom and flexibility that the release of the self-service widgets feature for Product Recommendations provides you as...

Dylan Walker | Jan 14, 2025

My blog is now running using Optimizely CMS!

It's official! You are currently reading this post on my shiny new Optimizely CMS website.  In the past weeks, I have been quite busy crunching eve...

David Drouin-Prince | Jan 12, 2025 | Syndicated blog

Developer meetup - Manchester, 23rd January

Yes, it's that time of year again where tradition dictates that people reflect on the year gone by and brace themselves for the year ahead, and wha...

Paul Gruffydd | Jan 9, 2025