How to: Get the available choices from a choice content type
Posted
Thursday, March 13, 2008 3:56 PM
by
CoreyRoth
I was recently building an ASP.NET control that needed to display a DropDownList with the same choices coming from a Choice content type. It is pretty easy to get access to a content type, but getting the available choices for the content type requires a cast. The first thing to know is some UI to API translation. Site Column in the UI is referred to as a Field in the API. Here is the code.
using (SPWeb currentSite = SPContext.Current.Web)
{
MyDropDownList.DataSource = ((SPFieldChoice)currentSite.Fields["MyChoiceSiteColumn"]).Choices;
MyDropDownList.DataBind();
}
The Fields collection will return you an SPField object but you need to cast it to an SPFieldChoice object to get to the collection called Choices. This is just a simple string collection that can be bound to a DropDownList or whatever. Hopefully, this helps if you ever need to get access to the choices of this content type.