Getting all values from a Choice field in SharePoint 2010 using JavaScript


I recently ran into an issue where I needed to get possible values for a SharePoint 2010 choice column.  Thought I would share the code in case anyone else runs across this need.  This code uses the ECMAScript Client-Side Object Model. 

 

  1. //Variable to hold the value once returned from SharePoint client.svc
  2.     var distinctChoices = [],
  3.         // Function to return all of the choices from the Choice column passed in
  4.         getUniqueColumnValuesFromChoice = function (columnName) {
  5.             var ctx = new SP.ClientContext.get_current();
  6.             var web = ctx.get_web();
  7.             var list = web.get_lists().getByTitle('Your List Here');
  8.             var field = list.get_fields().getByInternalNameOrTitle(columnName);
  9.             var choiceField = ctx.castTo(field, SP.FieldChoice);
  10.  
  11.             ctx.load(field);
  12.             // Call executeQueryAsync and pass in success and failure anonymous functions
  13.             ctx.executeQueryAsync(function () {
  14.                 // Return array of all of the choices in the choice field
  15.                 distinctChoices = choiceField.get_choices();
  16.                 // Since call is asynchronous, call function to interact with the choices
  17.                 onFieldLoaded();
  18.             },
  19.             // This will be called when executeQueryAsync fails
  20.             function (sender, args) {
  21.                 console.log(args);
  22.             });
  23.         };
  24.  
  25.     function onFieldLoaded() {
  26.         // Not called until choices are loaded into distonctChoices array
  27.     };

Comments (7)

  1. How do you call this function? says:

    How do you call this function?

    I tried getUniqueColumnValuesFromChoice()

    and I tried to insert this code in the pageload, but it doesn't work, I had to rewrite the function to call it

    how do you call it?

  2. getUniqueColumnValuesFromChoice('ColumnName') so if the column name was Colors, you would call it like this:

    getUniqueColumnValuesFromChoice('Colors')

    You should be able to use the internal name or the column title.

  3. Madhan says:

    How to use this script in the content editor webpart  and bind the choice value in  to  the html drop down?

  4. Inderjeet Singh says:

    works fine, Thanks a lot bro.

    Call it this way,

    getUniqueColumnValuesFromChoice('MyChoiceColumn');

    and Change the first line to this one.

    function getUniqueColumnValuesFromChoice(columnName) {

    }

  5. Joel dsouza says:

    I want to dynamically add choices based on certain conditions, how do i flush the data before adding new ones using this code?

  6. SC Vinod says:

    Hi , Can we cast the field to SP.FieldLookup to retrieve values from a lookup field?

  7. Hi says:

    I am very new client script : can any body help me.

    I am using below to execute but its not comming.. please help me

    <script type="text/javascript">

    // Setup context and load current web

    var context = new SP.ClientContext.get_current();

    var web = context.get_web();

    context.load();

    // Get task list

    var taskList = web.get_lists().getByTitle("Tasks");

    // Get Priority field (choice field)

    var priorityField = context.castTo(taskList.get_fields().getByInternalNameOrTitle("Priority"),

                                      SP.FieldChoice);

    // Call server

    context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),

                             Function.createDelegate(this, this.onFailureMethod));  

    function onSuccessMethod(sender, args) {

       // Get string arry of possible choices (but NOT fill-in choices)

       var choices = priorityField.get_choices();

       alert("Choices: (" + choices.length + ") – " + choices.join(", "));

    }

    function onFailureMethod(sender, args) {

       alert("oh oh!");

    }

    </script>

Skip to main content