Skip to content

Instantly share code, notes, and snippets.

@vgrem
Last active July 13, 2023 17:31
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save vgrem/8bc73080f42e1e11aa5b to your computer and use it in GitHub Desktop.
Save vgrem/8bc73080f42e1e11aa5b to your computer and use it in GitHub Desktop.
Example: how to execute SharePoint REST Batch request
var jsonSPHeaders = {
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"DataServiceVersion": "3.0"
};
OData.request( {
requestUri: _spPageContextInfo.webAbsoluteUrl + "/_api/$batch",
method: "POST",
headers: { "X-RequestDigest": $("#__REQUESTDIGEST").val(),
"DataServiceVersion": "3.0" },
data: { __batchRequests: [
{ requestUri: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Pages')/items", method: "GET" , headers: jsonSPHeaders },
{ requestUri: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Tasks')/items", method: "GET", headers: jsonSPHeaders }
]}
},
function (data, response) {
console.log('Pages list:');
printListItems(data.__batchResponses[0].data);
console.log('Tasks list:');
printListItems(data.__batchResponses[1].data);
},
null,
OData.batchHandler);
function printListItems(data){
data.results.forEach(function(item){
console.log(item.Title);
});
}
@davehax
Copy link

davehax commented May 16, 2018

Thank you for this

@mauryagit
Copy link

Can you please share snippets for master and child data insertion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment