I know the OData batch process built into SharePoint Online 2016 and O365 has not made it to SharePoint 2013 Online, but I have read a few places that say using a batch create/update IS possible in SP2013 by using the SP2010 _vti_bin/listdata.svc to do batch updates/inserts. In fact I have some code I use in Excel that does something similar by writing the entire range of data to SharePoint in a batch using the vti_bin/listdata.svc method, but I haven't figured out how to do this using the REST API yet as there are a lot of extra steps.
I have read postings on the SharePoint Stack Exchange from the following places that tell how to do it:
Cannot find resource for the request $batch Sharepoint server 2013
$batch operations on SharePoint 2013 on-premise not working
Except I cannot get it to work. I am following the code in this example and making the changes as needed looking at the pastebin example the one person posted here, but have been unsuccessful in getting it to work.
I am following the instructions from here: http://www.c-sharpcorner.com/UploadFile/780289/save-mutiple-records-to-sharepoint-2013-using-rest-api-in-a/
and from what they are saying it looks very similar to what he has in there.
Here is the code I am using:
this.batchUpdate = function (model, list, errorMsg) {
var batchContents = new Array();
var changeSetId = self.generateUUID();
var batchUuid = self.generateUUID();
var endpointUrl = SPUrl + '_vti_bin/listdata.svc/' + list
//create the changeSet
for (var index = 0; index < model.length; index++) {
batchContents.push('--changeset_' + changeSetId);
batchContents.push('Content-Type:application/http');
batchContents.push('Content-Transfer-Encoding: binary');
batchContents.push('');
batchContents.push('POST ' + endpointUrl + ' HTTP/1.1');
batchContents.push('Content-Type: application/json;odata=verbose');
batchContents.push('Accept: application/json');
batchContents.push('');
batchContents.push(JSON.stringify(model[index]));
batchContents.push('');
}
//End changeset to create Data
batchContents.push('--changeSetId_' + changeSetId + '--');
//generate batch body
var batchBody = batchContents.join('\r\n');
endpointUrl = SPUrl + '/_vti_bin/listdata.svc/$batch';
//create batch specific header
var batchRequestHeader = {
'X-RequestDigest': $("#__REQUESTDIGEST").val(),
'Content-Type': 'multipart/mixed; boundary="batch_' + batchUuid + '"'
};
return $.ajax({
url: endpointUrl,
type: 'POST',
headers: batchRequestHeader,
data: batchBody
}).then(function (data, request) {
return data.d;
}, function (error) {
errorMsg !== undefined && errorMsg !== null ? alertify.error(errorMsg + error.status) : null
});
}
Any help would be greatly appreciated, I really need to get this functionality working ASAP!!
batchBody
andbatchRequestHeader
after setting the value, does it look correct?