November Happy Hour will be moved to Thursday December 5th.

Aniket
Apr 27, 2018
  6128
(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
AsyncHelper can be considered harmful

.NET developers have been in the transition to move from synchronous APIs to asynchronous API. That was boosted a lot by await/async keyword of C#...

Quan Mai | Dec 4, 2024 | Syndicated blog

The search for dictionary key

Recently I helped to chase down a ghost (and you might be surprised to know that I, for most part, spend hours to be a ghostbuster, it could be fun...

Quan Mai | Dec 4, 2024 | Syndicated blog

Shared optimizely cart between non-optimizley front end site

E-commerce ecosystems often demand a seamless shopping experience where users can shop across multiple sites using a single cart. Sharing a cart...

PuneetGarg | Dec 3, 2024

CMS Core 12.22.0 delisted from Nuget feed

We have decided to delist version 12.22.0 of the CMS Core packages from our Nuget feed, following the discovery of a bug that affects rendering of...

Magnus Rahl | Dec 3, 2024

Force Login to Optimizely DXP Environments using an Authorization Filter

When working with sites deployed to the Optimizely DXP, you may want to restrict access to the site in a particular environment to only authenticat...

Chris Sharp | Dec 2, 2024 | Syndicated blog

Video Guides: Image Generation Features in Optimizely

The AI Assistant for Optimizely now integrates seamlessly with Recraft AI, providing advanced image generation capabilities directly within your...

Luc Gosso (MVP) | Dec 1, 2024 | Syndicated blog