EPiServer custom property dropdown
February 18th, 2011Some EPiServer developers still create their own custom property for the only purpose of having a simple dropdown (aka combobox / select list) for editors to choose from a predefined list.
If you only need a simple key/value dropdown, there’s no need to create a custom property – all you need is to configure it using appSettings in web.config.
UPDATE – As of EPiServer CMS 6 R2, the method described here is redundant since it has a built in dropdown property type. Thanks to Allan Thraen for the notice.
It’s highly recommended to reference an external file for the appSettings from web.config, which prevents the application from restarting every time it needs to be modified.
1 2 3 | <configuration> <appSettings configSource="AppSettings.config" /> </configuration> |
Create the AppSettings.config file and add a key with name and values separated with semicolon (;) and each pair separated with a pipe character (|).
1 2 3 4 | <?xml version="1.0"?> <appSettings> <add key="FavoriteColor" value="White;#fff|Black;#000|Blue;#0276FD" /> </appSettings> |
When you create the property, note that the name of your property has to be the same name as the appSetting key. The property type should be “Dynamic list (one option)” [PropertyAppSettings].
You could also create checkboxes if you use the type “Dynamic list (multimple options)” [PropertyAppSettingsMultiple].
Here’s a PageTypeBuilder example:
1 2 3 4 5 6 7 8 9 10 11 12 | [PageTypeProperty(EditCaption = "Favorite color", HelpText = "Select your favorite color", SortOrder = 100, UniqueValuePerLanguage = false, Searchable = false, Required = false, Type = typeof(PropertyAppSettings))] public virtual string FavoriteColor { get; set; } |
Tested in EPiServer CMS 5 and 6.