4

I need to do bulk inserts and learned that there is the $batch operation in the REST API. https://msdn.microsoft.com/en-us/library/office/dn903506.aspx

I read through this and following and explored his GitHub http://www.andrewconnell.com/blog/part-1-sharepoint-rest-api-batching-understanding-batching-requests https://github.com/andrewconnell/sp-o365-rest/blob/master/SpRestBatchSample/Scripts/App.js

I ended up creating a POST against my server that looks like this

POST http://portalstst.m.DUMMY.com/sites/DUMMY_digital/_api/web/lists/getbytitle('Logging_DB')/Items HTTP/1.1
Host: portalstst.m.DUMMY.com
Connection: keep-alive
Content-Length: 680
Origin: http://portalstst.m.DUMMY.com
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1
contentType: application/json;odata=verbose
Content-Type: multipart/mixed; boundary="batch_6495a5b4-f360-44d4-9a61-5a9d515456de"
Accept: application/json;odata=verbose
DataServiceVersion: 1.0
X-RequestDigest: 0x428391B9D35610B96821EEF1C768534C38C8CEB9A275EC8A661C3D3DE08A7DB4A342899235931BA589AA153430D484C0D413976B028C77292E2F8A77F27C5039,19 Aug 2016 14:17:46 -0000
DNT: 1
Referer: http://portalstst.m.DUMMY.com/sites/DUMMY_digital/App/index.html
Accept-Encoding: gzip, deflate
Accept-Language: de,en-US;q=0.8,en;q=0.6

--batch_6495a5b4-f360-44d4-9a61-5a9d515456de
Content-Type: multipart/mixed; boundary="changeset_9473c1bb-265d-45df-9ad1-9a0487d043ee"
Content-Length: 436
Content-Transfer-Encoding: binary

--changeset_9473c1bb-265d-45df-9ad1-9a0487d043ee
Content-Type: application/http
Content-Transfer-Encoding: binary

POST http://portalstst.m.DUMMY.com/sites/DUMMY_digital/_api/web/lists/getbytitle('Logging_DB')/Items HTTP/1.1
Content-Type: application/json;odata=verbose

{"__metadata":{"type":"SP.Data.Logging_x005f_DBListItem"},"FromPage":"transformation","ToPage":"capability"}

--changeset_9473c1bb-265d-45df-9ad1-9a0487d043ee--

--batch_6495a5b4-f360-44d4-9a61-5a9d515456de--

The result in short: HTTP/1.1 415 Unsupported Media Type, The HTTP header Content-Type is missing or its value is invalid.

HTTP/1.1 415 Unsupported Media Type
Cache-Control: private, max-age=0
Content-Type: application/json;odata=verbose;charset=utf-8
Expires: Thu, 04 Aug 2016 14:17:53 GMT
Last-Modified: Fri, 19 Aug 2016 14:17:53 GMT
Server: Microsoft-IIS/8.5
X-SharePointHealthScore: 0
SPClientServiceRequestDuration: 140
X-AspNet-Version: 4.0.30319
SPRequestGuid: 72559b9d-7727-4083-b167-b7fd1dca3fb6
request-id: 72559b9d-7727-4083-b167-b7fd1dca3fb6
X-RequestDigest: 0xB102AC2A481F112F017D042DC858F3429233A97163971A795C93E38BDC57FD7B04967CABD190D539844F91C21252BD64BD6589C5D90E114550C1D6FB119C96F1,19 Aug 2016 14:17:53 -0000
X-FRAME-OPTIONS: SAMEORIGIN
X-Powered-By: ASP.NET
MicrosoftSharePointTeamServices: 15.0.0.4763
X-Content-Type-Options: nosniff
X-MS-InvokeApp: 1; RequireReadOnly
Date: Fri, 19 Aug 2016 14:17:52 GMT
Content-Length: 177

{"error":{"code":"-1, Microsoft.SharePoint.Client.ClientServiceException","message":{"lang":"en-US","value":"The HTTP header Content-Type is missing or its value is invalid."}}}

I compared my request to the example at https://gist.github.com/andrewconnell/ad502b73b2cc9504229d and don't see any mistake.

Now I wonder whether this is supported on premise at all? Or what I'm doing wrong?

3

As far as I know REST API batching is only supported in SharePoint Online and SharePoint 2016. Support for REST API batching did not get pushed down to SharePoint 2013.

  • Is MSDN always that inaccurate? msdn.microsoft.com/en-us/library/office/dn903506.aspx states " Applies to: SharePoint Add-ins | SharePoint Online | SharePoint Server 2013" That's what made me start looking into that batch thing – Jens Aug 22 '16 at 6:44
  • Ok, reading sometimes helps. Despite the "Applies to:" line, later the article states "This article describes how you can batch queries and operations against the REST/OData API of Microsoft SharePoint Online (and on-premise SharePoint 2016 and later)". So I'm out of luck :-( – Jens Aug 22 '16 at 6:49
0

If I recall correctly you need Service Pack 1 at least for SharePoint 2013 for this to be supported.

  • According to the farm admins, we are running SP1. – Jens Sep 12 '16 at 11:29
0

Finally found a solution. Like in other cases where the REST API is missing something, fall back to the List Service and use it in pretty much the same way as is described for the REST variant in newer versions:

/_vti_bin/listdata.svc/$batch

Problem solved

  • Can you elaborate on this? Are you saying that you were able to get $batch to work for SP 2013 On-Premise by falling back to the 2010 REST endpoint variation? I tried such with no luck. Will you post a working example? – Josey Sep 9 '16 at 19:05
  • @Josey yes, falling back to the 2010 version does work on our On-premise farm. I pretty much followed the examples from andrewconnell to construct the batch http request and payload, see links in first post, and just used the listdata.svc – Jens Sep 12 '16 at 11:23
  • Could you share a working example please? I'm trying to use it but I receive "Error processing batch request. At the start of each change set, only two headers can be specified - 'Content-Type' and 'Content-Length'. Custom headers are allowed only within each operation, along with the standard HTTP headers." and I'm not able to figure out what's wrong in my request – AymKdn Oct 18 '16 at 14:25
  • @AymKdn see pastebin.com/2EMLXq5i – Jens Oct 20 '16 at 9:26
  • @Jens > thanks. At the end I won't be able to use listdata.svc because I need $batch on something not related to the Lists :-( – AymKdn Oct 20 '16 at 15:08

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.