13

I'm looking to bulk create items to decrease the amount of calls being made in my application. In this instance, I'm working on a calendar control that has different needs than what SharePoint OOTB provides (or allows you to modify). That said, if a user selects several days and wants to create list items for those days, it makes no sense to make separate calls per item.

However, I can't figure out the correct way to format the JSON object to send out. My call is returning the following in data.body: {\"error\":{\"code\":\"-1, Microsoft.SharePoint.Client.InvalidClientQueryException\",\"message\":{\"lang\":\"en-US\",\"value\":\"A node of type 'StartArray' was read from the JSON reader when trying to read the start of an entry. A 'StartObject' node was expected.\"}}}"

That error is true -- I'm pushing SharePoint objects and trying to send those out. Here is my code:

for (var i = 0; i <= diffDays; i++) {
    items.push({
        __metadata: { "type": "SP.Data.TestListItem" },
        OData__x0066_x20: userName(),
        qnlu: startDate.addDays(i),
        OData__x006e_ot5: status
    });
}

executor.executeAsync({
    url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + LIST_NAME + "')/items?@target='" + hostweburl + "'",
    method: "POST",
    body: JSON.stringify(items),
    headers: {
        "Accept": "application/json; odata=verbose",
        "content-type": "application/json; odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: onAddEventSuccess,
    error: onoDataCallFailure
});

And here is the JSON object I'm trying to POST (taken from Visual Studio debugging): [{\"__metadata\":{\"type\":\"SP.Data.TestListItem\"},\"OData__x0066_x20\":\"user\",\"qnlu\":\"2013-07-24T04:00:00.000Z\",\"OData__x006e_ot5\":\"test\"},{\"__metadata\":{\"type\":\"SP.Data.TestListItem\"},\"OData__x0066_x20\":\"user\",\"qnlu\":\"2013-07-24T04:00:00.000Z\",\"OData__x006e_ot5\":\"test\"},{\"__metadata\":{\"type\":\"SP.Data.TestListItem\"},\"OData__x0066_x20\":\"user\",\"qnlu\":\"2013-07-24T04:00:00.000Z\",\"OData__x006e_ot5\":\"test\"}]

Any help is appreciated.

7

Unfortunately, the REST API does not provide a way to batch items (yet). However, you can batch add items using the JavaScript Client Object Model. Here is a sample code:

var itemArray = [];

function createListItems() {

    var clientContext = SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('TestList');


    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() {

    alert('Items created');
    alert(itemArray[0].get_id());
    alert(itemArray[0].get_item("Title"));
}

function onQueryFailed(sender, args) {

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

createListItems();

Update

REST API supports batching now:

https://dev.office.com/sharepoint/docs/sp-add-ins/make-batch-requests-with-the-rest-apis

http://www.vrdmn.com/2016/06/sharepoint-online-get-userprofile.html

  • Thanks for this. What I really like about the REST API is that I get a collection of any added items back. Is there anyway to get that using JSOM? Also, any links to any decent documentation would be helpful. Everything on MSDN that I've found so far is extremely basic. – EHorodyski Jul 15 '13 at 19:13
  • Yes you do indeed get back the collection. You can access it using the itemArray. See the updated code. Also here is a MSDN link which I have found to be pretty useful for the JSOM docs: msdn.microsoft.com/en-us/library/hh185015(v=office.14).aspx – Vardhaman Deshpande Jul 15 '13 at 19:45
  • That isn't working for me. Where are you getting that code from? Should I be passing itemArray into onQuerySucceeded()? itemArray is undefined for me. Is Function.createDelegate() essential to getting that portion to work? – EHorodyski Jul 15 '13 at 20:05
  • Also, the page you posted is for SharePoint 2010. – EHorodyski Jul 15 '13 at 20:31
  • The code works as is for me. Are you sure you are defining itemArray outside the function? Maybe you can update your question with your code. Function.createDelegate() is not necessary for the code to run. Also, the link works for SP2013 too. The basics of the JSOM are same for SP2010 and SP2013 – Vardhaman Deshpande Jul 15 '13 at 21:40
6

Batching is now supported in REST O365 only. You can check out a javascript library on GitHub that batches these for you using the new capabilities.

https://github.com/SteveCurran/sp-rest-batch-execution

  • Will there be any updates for on-prem in the future? – Jordan Jan 20 '15 at 15:04
  • That looks promising. What about on-premise support? – Nadeem Yousuf Jan 20 '15 at 15:15

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.