Sharepoint - How to retrieve distinct values with JavaScript Client Object Model

Assuming that your field is a Choice/MultiChoice field without "Fill In Choices" enabled then you can use get_choices.

If it does have fill in choices and/or is a different field type then I don't think there is an option apart from brute force (iter over all items getting unique values).

http://spdailytips.blogspot.com/2011/11/retrieve-all-choice-field-values-using.html

<script type="text/ecmascript">

function GetChoiceValues() {
  var context = new SP.ClientContext.get_current();    
  var web = context.get_web();    
  context.load();

  // Get the list
  var taskList = web.get_lists().getByTitle("list");

  // Get Department choice field (choice field)
  var deptChoiceField = 
      context.castTo(taskList.get_fields().getByInternalNameOrTitle("department"),
                     SP.FieldChoice);

  context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),
                            Function.createDelegate(this, this.onFailureMethod)); 
}

function onSuccessMethod(sender, args) {
    var choices = deptChoiceField.get_choices();
    listBoxControl1.Items.Add(choices);
    alert("Choices: (" + choices.length + ") - " + choices.join(", "));
}

function onFailureMethod(sender, args) {
    alert("failed. Message:" + args.get_message());
}

</script> 

Tags:

Javascript