Skip to content

How to retrieve all choice field values using ecmascript – SharePoint 2010

July 10, 2012

I had a requirenment to get all the choice values in one of my choice colums in a list, and I want to add these values into a html dropdown on page load.

Just copy and paste the bellow code inside script tag and change ListName, ColumnName and Dropdown Id :

  • window.onload = retrieveListItems;
    var deptChoiceField;
  • function retrieveListItems() {
            varclientContext = SP.ClientContext.get_current();
            var webSite = clientContext.get_web();
            var lists = webSite.get_lists();
            var oList = lists.getByTitle(“ListName“);
    deptChoiceField = clientContext.castTo(oList.get_fields().getByInternalNameOrTitle(“ColumnName“),SP.FieldChoice);
    clientContext.load(deptChoiceField);
    clientContext.executeQueryAsync(
    Function.createDelegate(this, this.fillDropDown),
    Function.createDelegate(this, this.onListDataLoadQueryFailed));
    }
  • function onListDataLoadQueryFailed(sender, args) {
    alert(‘Request failed. ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
    }
  • function fillDropDown() {
    var choices = deptChoiceField.get_choices();
         var ddlCategory = document.getElementById(‘ddl‘);
         if (ddlCategory != null)
    {
                for (var i = 0; i < choices.length; i++)
    {
                      var theOption = new Option;
    theOption.text = choices[i];
    ddlCategory.options[i] = theOption;
    }
    }
    }

From → .Net

Leave a Comment

Leave a comment