6

I am creating a dropdown and want to fill it with choices with my choice column of SharePoint list. Looking for solution in CSOM or JSOM.

Any help is appreciated

2 Answers 2

3

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]);
 }

}
5

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));
        }

    });
1
  • Nice one. host url can be derived from _spPageContextInfo.webAbsoluteUrl. Nov 1, 2018 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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