Thursday, May 29, 2014

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

Folks,

In this post i will explain you how to retrieve choice field values in Client Side Object Model (CSOM) in SharePoint 2013. I have tested this code in Auto hosted app on SharePoint online environment. You can use this code snippet to get values from choice field for custom filter and search conditions.

 var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                List queslist = clientContext.Web.Lists.GetByTitle("YourListTitle");
                Field choice = queslist.Fields.GetByInternalNameOrTitle("FieldName");

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

You have to get the SharePoint context, then get the list field and then get that field choice and use the choice fields as per your needs, i have added the choice values in a drop-down list.

Let me know if you face any problem..

Happy Coding :)

No comments:

Post a Comment