none
Getting Selected Value of SharePoint Choice Column

    Question

  • I have a SharePoint Document Library and a Visual Web Part.  When the web part is called it will get the values from the document library and populate a form on the web part.

    In the library there is a choice column that allows mutliple values, and on my form there are 4 checkboxes.  What I'd like to do is get the selected values from the choice column and set my check boxes to checked or unchecked depending on if a certain choice values are selected.  Below is my best attempt at getting this accomplished, however, my code only returns the possible choices, not the selected values.  I need help figuring out how to get the selected values from the list.  Any help is greatly appreciated because I've combed through Bing search results and researched the MSDN library about as much as I can.  Maybe I'm missing something because this seems like it should be an easy thing to figure out.

    SPFieldMultiChoice getValues = (SPFieldMultiChoice)SpList.Fields.GetField("FieldName");
                        if (getValues.Choices.Contains("Choice 1"))
                        {
                            this.formCheckBox1.Checked = true;
                        }
                        if (getValues.Choices.Contains("Choice 2"))
                        {
                            this.formCheckBox2.Checked = true;
                        }
                        if (getValues.Choices.Contains("Choice 3"))
                        {
                            this.formCheckBox3.Checked = true;
                        }
                        if (getValues.Choices.Contains("Choice 4"))
                        {
                            this.formCheckBox4.Checked = true;
                        }

    Friday, October 26, 2012 3:49 AM

Answers

  • Hi ajmohrf,

    Please use the following code:

    // Get a reference to the field.
    SPFieldMultiChoice choiceField = (SPFieldMultiChoice)list.Fields.GetField("fieldInternalName");
    // Get the value of the field in the item.
    string rawValue = item[choiceField.Id].ToString();
    SPFieldMultiChoiceValue typedValue = new SPFieldMultiChoiceValue(rawValue);
    for (int i = 0; i < typedValue.Count; i++)
    {
        //complete the code
    }
    
    
    

    Please replace "fieldInternalName" with the statics name of the field you mentioned.

    Thanks,
    Jinchun Chen


    Jinchun Chen
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff AT microsoft.com(Please replace AT with @)

    Monday, October 29, 2012 9:29 AM
    Moderator

All replies

  • HI,

    Try below code to get all checked value from library column:

    SPList list = web.Lists.TryGetList("Test"); if (list != null) { SPListItem item = list.Items[0]; SPFieldMultiChoiceValue choices = new SPFieldMultiChoiceValue(item["MultiChoice"].ToString()); for (int i = 0; i < choices.Count; i++) { Console.WriteLine(choices[i]); ///here you need to write code for your form checkbox } }

    Check this MSDN article for more info: http://msdn.microsoft.com/en-us/library/ms415672.aspx

    Hope it could help


    Hemendra: "Yesterday is just a memory,Tomorrow we may never see"

    Whenever you see a reply and if you think is helpful, click "Alternate TextVote As Helpful"! And whenever you see a reply being an answer to the question of the thread, click "Alternate TextMark As Answer

    Friday, October 26, 2012 4:58 AM
    Moderator
  • Hi, thank you for your response.  When I try that code I get an error that says Object Reference not set to an Instance of the Object.  Below is the exact code I"m using.  I can't seem to see anything wrong with it.

    SPList chronicClinicSpList = web.Lists["Chronic Clinic Documents"];
                        SPListItem li = chronicClinicSpList.GetItemById(docID);
                        SPFieldMultiChoiceValue getAsthmaValues = new SPFieldMultiChoiceValue(li["Asthma"].ToString());
                        for (int i = 0; i < getAsthmaValues.Count; i++)
                        {
                            string choice = getAsthmaValues[i];
                        }

    Friday, October 26, 2012 3:20 PM
  • Hi ajmohrf,

    Please use the following code:

    // Get a reference to the field.
    SPFieldMultiChoice choiceField = (SPFieldMultiChoice)list.Fields.GetField("fieldInternalName");
    // Get the value of the field in the item.
    string rawValue = item[choiceField.Id].ToString();
    SPFieldMultiChoiceValue typedValue = new SPFieldMultiChoiceValue(rawValue);
    for (int i = 0; i < typedValue.Count; i++)
    {
        //complete the code
    }
    
    
    

    Please replace "fieldInternalName" with the statics name of the field you mentioned.

    Thanks,
    Jinchun Chen


    Jinchun Chen
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff AT microsoft.com(Please replace AT with @)

    Monday, October 29, 2012 9:29 AM
    Moderator