SharePoint 2013 - SPLessons

Get Choice Field values using Client Si...

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Get Choice Field values using Client Side Object Model in SharePoint 2013

Get Choice Field values using Client Side Object Model in SharePoint 2013

Description: Hi every one, today we are going to see how to retrieve the choice field values of SharePoint List column using to SharePoint Client Object Model. Today while i was working the windows forms i got a requirement to bind the Job Title choice field values to the form job title drop-down field. So we will see how i achieved it using client object model.

  1. First i am getting the ClientContext Object.
  2. Second I am getting the List Object using the client context object.
  3. If you see the line number 5 i am passing the internal name of my choice field. You know we can pass the internal name or title of the SharePoint Choice field.
  4. If you see the line numbers 10 to 14 here i am adding the choice field values to form drop-down control.

 public void getListChoiceField(string list_name, string req_query, ComboBox control_name)
        {
            ClientContext context = new ClientContext(source_site_url);
            List list = context.Web.Lists.GetByTitle(list_name);
            Field choice = list.Fields.GetByInternalNameOrTitle("jobtitle");

            FieldChoice fldChoice = context.CastTo<FieldChoice>(choice);
            context.Load(fldChoice, f => f.Choices);
            context.ExecuteQuery();
            foreach (string item in fldChoice.Choices)
            {
                control_name.Items.Add(item.ToString());
                //add choices to dropdown list
            }
        }