How to get choices from choice field programmatically in SharePoint

This SharePoint tutorial explains, how to get choices from choice field programmatically in SharePoint using SharePoint server object model.

SharePoint get choices from a choice field programmatically

The below code represents that, there is a SharePoint Document Library in a SharePoint Site. In that library, there are two choice columns are presented as: “Language” and “Department“.

In both the choice field column, it has some choice options values. So by using this below C# Code (SharePoint server object model), we can get choices from choice field programmatically in SharePoint.

using Microsoft.SharePoint;
using System;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace TSInfo_Intranet.File_Upload
{
    public partial class File_UploadUserControl : UserControl
    {
        string siteURL = "";
        string listTitle = "Company Document";
        string libraryLanguageColumnName = "Language";
        string libraryDepartmentColumnName = "Department";
        SPSite siteCollection = SPContext.Current.Site;
        SPWeb web = SPContext.Current.Web;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetChoiceLanguageColumn();
                GetChoiceDepartmentColumn();
            }
        }
        
        public void GetChoiceLanguageColumn()
        {
            try
            {
                SPList spList = web.Lists.TryGetList(listTitle);
                SPFieldChoice chFldDocLanguage = (SPFieldChoice)spList.Fields[libraryLanguageColumnName];
                if (chFldDocLanguage != null)
                {
                    for (int i = 0; i < chFldDocLanguage.Choices.Count; i++)
                    {                       ddlLanguage.Items.Add(chFldDocLanguage.Choices[i].ToString());
                    }
                }
                else
                {
                    lblMessage.Text="Something went wrong";
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }
        public void GetChoiceDepartmentColumn()
        {
            try
            {
                SPList spList = web.Lists.TryGetList(listTitle);
                SPFieldChoice chFldDocDepartment = (SPFieldChoice)spList.Fields[libraryDepartmentColumnName];
                if(chFldDocDepartment !=null)
                {
                    for (int i = 0; i < chFldDocDepartment.Choices.Count; i++)
                    {
                        ddlDepartment.Items.Add(chFldDocDepartment.Choices[i].ToString());
                    }
                }
                else
                {
                    lblMessage.Text = "Something went wrong. Kindly contact with your administrator.";
                }
            }
            catch (Exception exc)
            {
                lblMessage.Text=exc.Message;
            }
        }
    }
}

You may like following SharePoint server object model tutorials:

This SharePoint tutorial explains, how to get choices from choice field programmatically in SharePoint using SharePoint server object model.

>