Saturday, 26 November 2011

Retrieve all choice field values using Ecmascript \Javascript Client object model - SharePoint 2010


I had a requirenment the other day to get all the choice values in one of my choice colums in a list. I needed to add these values into a html listbox. Here is the code to do just that

<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>

Ads by Google

1 comment:

  1. But, how to retrieve the current value of column choice of a list item???

    ReplyDelete