Sharepoint - How to get a list of all Choice field options using Client Object Model

You can get this from SP.FieldChoice.get_choices() (SPFieldChoice inherits SPFieldMultiChoice)

To get that object you need to use context.castTo to cast SP.Field to SP.FieldChoice.

// Setup context and load current web
var context = new SP.ClientContext.get_current();
var web = context.get_web();
context.load();

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

// Get Priority field (choice field)
var priorityField = context.castTo(taskList.get_fields().getByInternalNameOrTitle("Priority"),
                                   SP.FieldChoice);

// Load the field
context.load(priorityField);

// Call server
context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),
                          Function.createDelegate(this, this.onFailureMethod));  

function onSuccessMethod(sender, args) {
    // Get string arry of possible choices (but NOT fill-in choices)
    var choices = priorityField.get_choices();
    alert("Choices: (" + choices.length + ") - " + choices.join(", "));
}

function onFailureMethod(sender, args) {
    alert("oh oh!");
}

On SharePoint 2013, we can use the REST API. An example would be with a GET request similar to this:

/_api/web/lists/getbytitle('task')/Fields?$filter=Title eq 'Fieldname'

The request returns the field metadata. For more detail, view this link:

http://www.ozkary.com/2015/10/sharepoint-choice-field-options-with-angularjs.html?m=1

Tags:

Javascript