0

I am using the below code to insert an item into the list.

  function createitem() {
var selectListBox = document.getElementById("txtFormName");
var selectedListTitle = selectListBox.value;
var selectedList = web.get_lists().getByTitle(selectedListTitle);

var listItemCreationInfo = new SP.ListItemCreationInformation();
var newItem = selectedList.addItem(listItemCreationInfo);

newItem.set_item('Title','abc');
newItem.update();
context.load(newItem);
context.executeQueryAsync(onItemCreationSuccess, onItemCreationFail);

}

Its working fine. Now I want to use

    newItem.set_item('Title1','def');
    newItem.set_item('Title2','xyz');
    .
    .
    .
    and so on

in loop for more than one item in the same row to insert. How can i achieve that. kindly guide.

  • Are you asking how to insert the same item multiple times? – Amal Hashim Nov 26 '14 at 17:26
  • @AmalHashim no. field name and value both will be different. I have edited my question. – Mahatma Aladdin Nov 26 '14 at 17:28
0

You can use a loop as follows

var selectListBox = document.getElementById("txtFormName");
var selectedListTitle = selectListBox.value;
var selectedList = web.get_lists().getByTitle(selectedListTitle);

for(var index = 0; index < 10; index++) {
    var listItemCreationInfo = new SP.ListItemCreationInformation();
    var newItem = selectedList.addItem(listItemCreationInfo);
    newItem.set_item('Title' + index.toString(),'abc'+ index.toString());
    newItem.update();
}

context.load(selectedList);
context.executeQueryAsync(onItemCreationSuccess, onItemCreationFail);
  • Thanx Amal. Exactly what I needed. Just one thing: var listItemCreationInfo = new SP.ListItemCreationInformation(); var newItem = selectedList.addItem(listItemCreationInfo); will be above the for loop. :) cheers – Mahatma Aladdin Nov 26 '14 at 17:48

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.