3

I'm using this approach to bulk SP.ListItem creation:

var ctx = SP.ClientContext.get_current();
var oList = ctx.get_web().get_lists().getByTitle('listTitle');
myObj.forEach(function(d,i)
{
    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo); 

        fields.forEach(function(f){
            this.oListItem.set_item(f, d[f]);
        });

    this.oListItem.update();

    ctx.load(this.oListItem);
});


ctx.executeQueryAsync(
    Function.createDelegate(this, successHandler),
    Function.createDelegate(this, errorHandler)
);

function successHandler(data) {
    window.data = data; //==> see picture below
}

function errorHandler(s,c,e) {
    console.log("==ERR==", s,c,e)
}

//references: 
//http://www.vrdmn.com/2013/07/batch-operations-using-javascript.html
//https://msdn.microsoft.com/en-us/library/office/dn168907.aspx

enter image description here

Can I rely on data.$1L_0 property as the container of response object items? Is there a strict JSOM approach to get the ids?

Thanks!

1

You can check the ids from data.$1L_0 with the actual ids from your list. If they are same, then you can rely on data.$1L_0.

Anyway following JSOM approach, you can flow:

Add items in a array, then get ids from it using get_id().

var itemArray = [];
var clientContext = SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('Project');


for (var i = 0; i < 5; i++) {

    var itemCreateInfo = new SP.ListItemCreationInformation();
    var oListItem = oList.addItem(itemCreateInfo);
    oListItem.set_item('Title', 'My New Item!' + i);
    oListItem.update();
    itemArray[i] = oListItem;
    clientContext.load(itemArray[i]);
}

clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);


function onQuerySucceeded() {
    itemArray.forEach(function(item) {
        console.log(item.get_id());
    });
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

PS: Above example is already given in Vardhaman Deshpande's blog. I have just added the following section.

itemArray.forEach(function(item) {
        console.log(item.get_id());
    });

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.