Search code examples
asp.net-mvcvisual-studio-2010sharepoint-2010web-parts

How do I get Choice Values from a Document library's Choice column in code


I am fairly new to SharePoint development and as you may all know that it is very basic for one to know how to access fields in a choice column...

My problem: I want to access the values of the Check Boxes from a Choice Column.

For Example: I have a document library called Libe, this document library has a custom column with type Choice and has 4 checkboxes with the values:

  1. Category 1
  2. Category 2
  3. Category 3
  4. Category 4

How do I get the values like literally the text values of what is in the Check Box List: "Category 1", "Category 2" ... "Category 4".

Any ideas?

I can access the column fine and get the selected values, I just do not know how to get the values the user can choose from.

Answer

 SPFieldMultiChoice Fld = (SPFieldMultiChoice)list.Fields["Column"];
                                List<string> fieldList = new List<string>();
                                foreach (string str in Fld.Choices)
                                {
                                    fieldList.Add(str);


     }

Above is the answer, I can't answer my own question until I have a 100 rep.


Solution

  • using (SPSite site = new SPSite("http://servername/"))
            {              
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists["ListName"];
                        string values = list["yourColumn"] as string;
                        string[] choices = null;
                         if (values != null)
                           {
                                  choices = values.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);
                           }
                    } 
            }
    

    You can try this code for getting choice field value from document library.