The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.
The ability to add custom fields to an existing content module in Sitefinity CMS provides flexibility to achieve a various range of scenarios.
One of these fields is the Choice Field. It allows users to select one of the predefined values, which can later be accessed via code, and, for example, provide filtering based on this field on the frontend. However, if we want to have a drop down containing all the possible values of the field, they need to be hardcoded manually.
To avoid doing this and to prevent typos, we can use the API to tap directly into the definitions and pull all the values from there, allowing us to bind them to a drop down for example.
First, we need to get one of the backend views (Edit/Insert) and from there we will have access to the CustomFieldsSection and the field we are after.
using
System;
using
System.Linq;
using
Telerik.Sitefinity.Configuration;
using
Telerik.Sitefinity.Modules.Libraries.Configuration;
using
Telerik.Sitefinity.Web.UI.ContentUI.Views.Backend.Master.Config;
using
Telerik.Sitefinity.Web.UI.Fields.Config;
var conf = Config.Get<LibrariesConfig>();
var viewDef = conf.ContentViewControls[
"DocumentsBackend"
].ViewsConfig[
"DocumentsBackendEdit"
]
as
DetailFormViewElement;
var section = viewDef.Sections[
"CustomFieldsSection"
];
var mcFieldDefinition = section.Fields[
"CustomField1"
] asChoiceFieldElement;
var choices = mcFieldDefinition.Choices;
List<
string
> collection =
new
List<
string
>();
foreach
(var item
in
choices)
{
colletion.Add(item.Text);
}
Dropdownlist1.DataSource = collection;
Dropdownlist1.DataBind();
Stanislav Velikov is a Tech Support Engineer at Telerik. He joined the Sitefinity Support team in April 2011.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites
Progress collects the Personal Information set out in our Privacy Policy and the Supplemental Privacy notice for residents of California and other US States and uses it for the purposes stated in that policy.
You can also ask us not to share your Personal Information to third parties here: Do Not Sell or Share My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.