Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

How can you get distinct values from a SharePoint List column using JavaScript without a huge performance hit?

A simple example would be appreciated :)

share|improve this question
    
How many items you suppose to query? (maximum count) –  Kai Jan 18 '12 at 22:16
    
Whats the field type - is it a Choice/MultiChoice (and do you allow 'fill in' values) or something else? –  Ryan Jan 19 '12 at 8:28
    
At the moment I'm looking at 100+ items, but that could get a lot more over time id recon. In some lists I have choice fields Ryan, other there is People Picker fields –  Anders Aune Jan 19 '12 at 11:15

2 Answers 2

up vote 3 down vote accepted

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> 
share|improve this answer
    
Thank you, this will help me a lot on the way :) –  Anders Aune Jan 19 '12 at 11:16

I personally prefer /_vti_bin/listdata.svc. For example to retrieve only Title och Status from 100 first Tasks:

http://dev/_vti_bin/listdata.svc/Tasks()?$select=Title, StatusValue&top=100

See more on my blog: $select, $.getJSON and listdata.svc, updating with listdata.svc

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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