1

I am trying to insert a Multichoice (checkbox) field to sharepoint via the SP namespace in javascript.

Updatnig single choice is simple.

For Multichoice I have tried:

var itemCreateInfo = new SP.ListItemCreationInformation();
newListItem = list.addItem(itemCreateInfo);
var checked = $('input[name=' + field.internalName + 'group]:checked');
var value = new Array();
checked.each(function (i, item) {
   value.push(item.getAttribute('value'));
})
newListItem.set_item(field.internalName, value);

newListItem.update();
context.load(newListItem);
context.executeQueryAsync(onInsertSucceeded, onInsertFailed);

so value with set_item it is trying to upload a array, I have also tried making it a string separating values with a ';' Both options through an error

0
var item;
function insertItem()
{
   var itemCreateInfo = new SP.ListItemCreationInformation();
   newListItem = list.addItem(itemCreateInfo);
   var checked = $('input[name=' + field.internalName + 'group]:checked');

   // loop through the choices and concat with ','
   newListItem.set_item(field.internalName, 'Choice1, Choice 2');
   newListItem.update();

   context.executeQueryAsync(onInsertSucceeded, onInsertFailed);

}
function onInsertSucceeded() {
        alert("Item updated");
    }
function onInsertFailed() {
        alert('Failure!');
    }
  • Just curious @michael, but how did this answer help you? Using ',' should work just as fine as Array. I think the real problem is getting values from checked, which is not shown at all here – eirikb Apr 9 '14 at 5:58
  • Not sure why the questioner liked my answer.. I prefer your's too.. – Arsalan Adam Khatri Apr 9 '14 at 5:59
6

I just did a simple test, and this code works just fine:

var context = SP.ClientContext.get_current();
var item = context.get_web().get_lists()
  .getByTitle('Test')
  .addItem(new SP.ListItemCreationInformation());
item.set_item('MyField', ['Hello', 'World']);
item.update();
context.executeQueryAsync();

And this is quite identical to your code, so that makes me wonder if your variable value contains correct data. Could you set a breakpoint or simply console.log the variable after your loop?

One way to get the text of the checkboxes is to go for the labels, if you have them, like this:

var values = $('input:checked').map(function(){ 
  return $(this).next('label').text(); 
}).toArray();
  • @erikb I have same issue.. but your approach not working in SP2016 on premise – ujjaval May 11 '17 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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