Take the community feedback survey now.

sunylcumar
May 12, 2025
  899
(3 votes)

Creating a Dropdownlist in Optimizely CMS: Populate ISelectionFactory with values from another block's properties

Create a Block to hold selection options

using EPiServer.Cms.Shell.UI.ObjectEditing.EditorDescriptors;
using EPiServer.PlugIn;
using EPiServer.Shell.ObjectEditing;
using System.ComponentModel.DataAnnotations;
namespace MyApp.Models.Blocks
{
    [ContentType(DisplayName = "Select Options Block", Description = "")]
    public class SelectOptionsBlock : BlockData
    {
        [CultureSpecific]     
        [Display(Name = "Selection Options", Description = "List of options for the selection list.",  GroupName = SystemTabNames.Content, Order = 100)]
        [EditorDescriptor(EditorDescriptorType = typeof(CollectionEditorDescriptor<SelectionOption>))]
        public virtual IList<SelectionOption> SelectionOptions { get; set; }
    }

    public class SelectionOption
    {
        public string Text { get; set; }
        public string Value { get; set; }
    }

    [PropertyDefinitionTypePlugIn]
    public class SelectionOptionProperty : PropertyList<SelectionOption> { }

}

Create a page where you want to have the selection list and add the following 2 properties

namespace MyApp.Models.Pages
{
    [ContentType(DisplayName = "SelectionList Page", Description = "Page with a selection list populated from a block.", GroupName = SiteGroupNames.Specialized)]
    public class SelectionListPage : PageData
    {
        [CultureSpecific]
        [Display(Name = "Select Options Block", Description = "", GroupName = SystemTabNames.Content, Order = 1)]
        [AllowedTypes(typeof(SelectOptionsBlock))]
        public virtual ContentReference SelectOptionsBlockContent { get; set; }

        [CultureSpecific]
        [Display(Name = "Select List", Description = "",  GroupName = SystemTabNames.Content,  Order = 1)]
        [SelectOne(SelectionFactoryType = typeof(ItemSelectionFactory))]
        public virtual string SelectList { get; set; }
    }
}

Now create a selection list factory

using EPiServer.ServiceLocation;
using EPiServer.Shell.ObjectEditing;
using SVP.Models.Blocks;
namespace MyApp.Business.SelectionFactories
{
    public class ItemSelectionFactory : ISelectionFactory
    {
        public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
        {
            var loader = ServiceLocator.Current.GetInstance<IContentLoader>();
            var list = new List<SelectItem>();  
            dynamic contentMetadata = metadata;
            var ownerContent = contentMetadata.OwnerContent as IContent;            
            //create a dynamic object of current content type
            dynamic parentItem = ownerContent;
            //check if the block containing the select options is added to the current content (page or block)
            if (parentItem.SelectOptionsBlockContent != null)
            {
                //Get the block containing select options
                var optionsBlock = loader.Get<SelectOptionsBlock> parentItem.SelectOptionsBlockContent);
                //check if the optionsBlock has any items
                if (optionsBlock.SelectionOptions != null && optionsBlock.SelectionOptions.Count > 0)
                {
                    foreach (var listItem in optionsBlock.SelectionOptions)
                    {
                        list.Add(new SelectItem() { Text = listItem.Text, Value = listItem.Value });
                    }
                }
            }
            return list;
        }
    }
}

From the CMS, create a new block of type SelectOptionsBlock and add the necessary select options and publish it.

Select this newly created block in your page that you have created to have the selection list and publish the page.

May 12, 2025

Comments

El Magnifico
El Magnifico May 18, 2025 02:15 PM

Exellent example with full working source code.

Please login to comment.
Latest blogs
Optimizely CMS Mixed Auth - Okta + ASP.NET Identity

Configuring mixed authentication and authorization in Optimizely CMS using Okta and ASP.NET Identity.

Damian Smutek | Oct 27, 2025 |

Optimizely: Multi-Step Form Creation Through Submission

I have been exploring Optimizely Forms recently and created a multi-step Customer Support Request Form with File Upload Functionality.  Let’s get...

Madhu | Oct 25, 2025 |

How to Add Multiple Authentication Providers to an Optimizely CMS 12 Site (Entra ID, Google, Facebook, and Local Identity)

Modern websites often need to let users sign in with their corporate account (Entra ID), their social identity (Google, Facebook), or a simple...

Francisco Quintanilla | Oct 22, 2025 |

Connecting the Dots Between Research and Specification to Implementation using NotebookLM

Overview As part of my day to day role as a solution architect I overlap with many clients, partners, solutions and technologies. I am often...

Scott Reed | Oct 22, 2025

MimeKit Vulnerability and EPiServer.CMS.Core Dependency Update

Hi everyone, We want to inform you about a critical security vulnerability affecting older versions of the EPiServer.CMS.Core  package due to its...

Bien Nguyen | Oct 21, 2025

Speeding Up Local Development with a Fake OpenID Authentication Handler

When working with OpenID authentication, local development often grinds to a halt waiting for identity servers, clients, and users to be configured...

Eric Herlitz | Oct 20, 2025 |