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);
    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 Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: