Ram Prasad Meenavalli

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

Published on 12-Aug-2016 | Comments
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()); });


View Ram Prasad Meenavalli's LinkedIn profile

SharePoint Technology Evangelist