I am using the COM JavaScript/jQuery to get, create & update some list items. I have two lists and every time I create a new item in one list, the newly created id is something that I want to grab in order to place in a FK like field in the second list (not even using linked at this point, just a generic integer type field).

Simple enough, but what if I want to through this process into a loop and create 10 at the same time.

after I create a new item using

function createItem(tbl, obj, callback) {
    var cxt = new SP.ClientContext.get_current();
    var list = cxt.get_web().get_lists().getByTitle(list);
    var createInfo = new SP.ListItemCreationInformation();
    this.listItem = list.addItem(createInfo);
    for ( var prop in obj ) {
        listItem.set_item(prop, obj[prop]) 
    cxt.executeQueryAsync(callback, Function.createDelegate(this, this.onQueryFailed));


_createItem ('ListA', { Title: 'First List Item' }, function () {
      var thisId = listItem.get_id();
      var other = {
           Title: 'Second List Item',
           RelatedId: thisId
      _createItem('ListB', other, function () {

When I use the above inside a loop to create 5 records in both lists it doesn't work, the thisId/item.get_() typically returns a -1 even though the items are getting created in both lists. I am sure there is a perfectly good reason this occurs, I am just unsure of it and was hoping someone could help me understand, and perhaps if there is a way to get at what I am trying to do properly. I have tried deep copies and this doesn't seem to matter?


Due to async nature of JSOM I would recommend this approach.

The example demonstrates how to create multiple list items using JSOM:

//Create multiple contact items using Named Function Expressions (https://stackoverflow.com/a/7065175/1375553)
var createContactItems = function createContactItem(i,itemsCount)
     function(item) {
          console.log('Contact have been created');        
          var nextItemId = item.get_id() + 1;
          contactProperties.Title = 'Doe' + nextItemId;
          if(i <  itemsCount) {
     function logError(sender, args) {

var numberOfContacts = 10;


function createListItem(listTitle,itemProperties,success,error)
    var context = new SP.ClientContext.get_current();
    var list = context.get_web().get_lists().getByTitle(listTitle);
    var itemCreateInfo = new SP.ListItemCreationInformation();
    var listItem = list.addItem(itemCreateInfo);
    for(var propName in itemProperties) {
       listItem.set_item(propName, itemProperties[propName]) 
      function() {
| improve this answer | |

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.