Aniket
Apr 27, 2018
  6158
(2 votes)

Dynamic Drop down list editable by content authors using ISelectionFactory & Property List

On one of my recent projects, we wanted the ability for content authors to manage a list of items in the dropdown through the CMS. Though, it's simple to create a list of items that can be retrieved from a constants class or an appsetting.config, it does require developer to make that change. This also means there needs to be a deployment to production for a simple name/value change in a dropdown.

To get around this standard implementation, we implemented a simple Property List on the Start Page and referenced the property in the ISelectionFactory implementation to get the key value pair.

The dropdown list can now be managed by the content authors without relying on the developer to make a change.  

Here's the code:

 public class AccountPropertiesFactory : ISelectionFactory
    {
        public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
        {
            var contentRepository = ServiceLocator.Current.GetInstance<IContentLoader>();
            var startPage = contentRepository.Get<StartPage>(ContentReference.StartPage);

            var selectItems = new List<SelectItem>();

            foreach(var accountProperty in startPage.AccountTypePropertyList)
            {
                selectItems.Add( new SelectItem()
                    {
                        Text = accountProperty.Text,
                        Value = accountProperty.Text
                    }
                );
            }

            return selectItems.ToArray();
        }
    }
Apr 27, 2018

Comments

Praful Jangid
Praful Jangid Nov 20, 2020 01:14 PM

Thanks Aniket

You saved my day...

Please login to comment.
Latest blogs
Copy Optimizely SaaS CMS Settings to ENV Format Via Bookmarklet

Do you work with multiple Optimizely SaaS CMS instances? Use a bookmarklet to automatically copy them to your clipboard, ready to paste into your e...

Daniel Isaacs | Dec 22, 2024 | Syndicated blog

Increase timeout for long running SQL queries using SQL addon

Learn how to increase the timeout for long running SQL queries using the SQL addon.

Tomas Hensrud Gulla | Dec 20, 2024 | Syndicated blog

Overriding the help text for the Name property in Optimizely CMS

I recently received a question about how to override the Help text for the built-in Name property in Optimizely CMS, so I decided to document my...

Tomas Hensrud Gulla | Dec 20, 2024 | Syndicated blog

Resize Images on the Fly with Optimizely DXP's New CDN Feature

With the latest release, you can now resize images on demand using the Content Delivery Network (CDN). This means no more storing multiple versions...

Satata Satez | Dec 19, 2024

Simplify Optimizely CMS Configuration with JSON Schema

Optimizely CMS is a powerful and versatile platform for content management, offering extensive configuration options that allow developers to...

Hieu Nguyen | Dec 19, 2024

Useful Optimizely CMS Web Components

A list of useful Optimizely CMS components that can be used in add-ons.

Bartosz Sekula | Dec 18, 2024 | Syndicated blog