This blog will help to create single or multiple list options in CMS 12. Dropdown lists play an important role whenever we give the user to select a single value from the list.
In CMS 12, single or multiple selection lists populate using “SelectOne” and “SelectMany” attributes. This attribute is in the “EPiServer.Shell.ObjectEditing” namespace. You can define these attributes on a property and require a reference to a class implementing the “ISelectionFactory” interface. I referred to the Optimizely developer site.
Creating Dropdown Lists using Enum
- Create Enum Region.
- Create property Region with Enum type Region.
- Create a class named “SelectOneEnumAttribute” and inherit it from “SelectOneAttribute.”
- Also, implement this class with “IDisplayMetadataProvider.” In CMS 11, we need to implement with interface “IMetadataAware.” But in CMS 12, we need to implement “IDisplayMetadataProvider.”
- The method “CreateDisplayMetadata” is not virtual; hence we need to write a new keyword to override this method from SelectOneAttribute. Note that we were calling the base method as well.
- After writing all the code, we will get the below output
- If we decorate a property “Region” with “SelectManyEnum,” then we will get below output
Create Dropdown Lists using Database
- Create a property “OtherCities” of string type.https://blogs.perficient.com/files/Property.png
- Decorate this property with “SelectMany” and give the selection factory type as “CitySelectionFactory”
- Create a class “CitySelectionFactory” and implement it with the “ISelectionFactory” interface.
- In this class, we need to get all cities from the Database. Write the logic in the “GetSelection” method and return the list to do that.
- Create the “SelectManyEnumAttribute” Class and inherit it with the “SelectManyAttribute” class, and the same as Select Single Attribute implements with the “IDisplayMetadataProvider” interface.
- The method “CreateDisplayMetadata” is not virtual; hence we need to write a new keyword to override this method from SelectManyAttribute. Note that we were calling the base method as well.
- After writing all code, we will get the following output.
- If we decorate a property “OtherCities” with “SelectOne,” we will get below output.
Good , Very informative.
Very Informative and useful concept of opti CMS
Very good and informative.
Good piece of Information, helpful for those who are Implementing dropdowns in Optimizely CMS12.
Nice blog Prasad, keep posting such informative content!
Great going Prasad.
Nice & Informative blog
Good , Very informative.