Ram Prasad
 

SharePoint JS CSOM Code to add/update Choices for a Choice Field

Aug, 12 2016
 
1 min/s
 
 

Following code snippet shows how to update the 'Status' column within a Tasks list and add new choice values to the field/column using JS CSOM. The Status column within a Tasks list is of type Choice Field.

var CONST_TASKS_LIST_TITLE = "Tasks";
var CONST_TASKS_LIST_COLUMN = "Status";
var strStatusValues = "Deferred,Closed,Blocked,Postponed for next Release";

var ctx = SP.ClientContext.get_current(); var listDevTasks = ctx.get_web().get_lists().getByTitle(CONST_TASKS_LIST_TITLE); var fldStatus = listDevTasks.get_fields().getByInternalNameOrTitle(CONST_TASKS_LIST_COLUMN) var fldStatusChoice = ctx.castTo(fldStatus, SP.FieldChoice); ctx.load(fldStatusChoice); ctx.executeQueryAsync(function () { var newValues = strStatusValues.split(","); var currentChoices = fldStatusChoice.get_choices(); for (var i = 0; i < newValues.length; i++) { currentChoices.push(newValues[i]); } fldStatusChoice.set_choices(currentChoices); fldStatusChoice.updateAndPushChanges(); ctx.executeQueryAsync(function () { console.log("Added new choice values to the column"); }, function (sender, args) { deferred.reject(args.get_message()); }); }, function (sender, args) { deferred.reject(args.get_message()); });