I have a list 'choiceOpt' which contains a choice field 'choiceOptions' having Yes ad No as options.

I want to retrieve this Yes and No programmatically (javascript) .

Kindly help.


2 Answers 2

function retrieveListItems(siteUrl) {
    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('choiceOpt');

    var camlQuery = new SP.CamlQuery();
        '<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
        '<Value Type=\'Number\'>1</Value></Geq></Where></Query>' + 
    this.collListItem = oList.getItems(camlQuery);

        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)

function onQuerySucceeded(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        var choiceValue = oListItem.get_item('choiceOptions');

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());

See here for more details: How to: Complete basic operations using JavaScript library code in SharePoint 2013


If you want to get the list of available choices programatically (as opposed to the actual value of a particular list item) you can do something like this:

(function() {
    var myCtx = SP.ClientContext.get_current(),
        myList = myCtx.get_web().get_lists().getByTitle("myList"),
        myField = myList.get_fields().getByInternalNameOrTitle("myField"),
        myFieldChoices = myCtx.castTo(myField, SP.FieldChoice);
    myCtx.executeQueryAsync(function() {
        // get_choices() here will output an array of strings representing each choice
        var myChoices = myFieldChoices.get_choices();
    }, function(sender, args) {
        // fail horribly for some reason

As noted in the comments, get_choices() will give you a plain JavaScript array of strings that represent the value of each choice for that field.

I like to use this as a way to retrieve valid choices to display in other areas of my applications (like filters, or groups, or even just generating my own choice options), since it is logically correct in the SharePoint list and fairly easy to retrieve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.