Sharepoint - How to retrieve choice column in sharepoint Online?
Using rest API, just make a GET request to the following end-point:
_api/web/lists/GetByTitle('List Name')/fields?$filter=EntityPropertyName eq 'Choice Field Name'"
Example using jQuery
$.ajax({
url: "host url"+"_api/web/lists/GetByTitle('List Name')/fields?$filter=EntityPropertyName eq 'Choice Field Name'",
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
success: function (data) {
console.log(data.d.results[0].Choices.results);
},
error: function (error) {
alert(JSON.stringify(error));
}
});
You can also try this using CSOM:
function GetChoiceValues()
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
this.taskList = web.get_lists().getByTitle("CMA");
this.fields= this.taskList.get_fields();
var deptChoiceField = context.castTo(this.taskList.get_fields().getByInternalNameOrTitle("Category"),SP.FieldChoice);
context.load(this.taskList);
context.load(this.fields);
context.load(deptChoiceField);
context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),Function.createDelegate(this,this.onFailureMethod));
}
function onFailureMethod(sender, args) {
alert("Error Occured: "+ args.get_message());
}
function onSuccessMethod(sender, args)
{
var context = new SP.ClientContext.get_current();
var myChoicesfield = context.castTo(this.fields.getByInternalNameOrTitle("Category"),SP.FieldChoice);
var choices = myChoicesfield.get_choices();
for(i=0;i<choices.length;i++)
{
//divHtml+="<option value='"+choices[i]+"'>"+choices[i]+"</option>";
console.log(choices[i]);
}
}