Web API Bulk Operations

Batch Operations using Microsoft Dynamic CRM Web API

In this blog post we will describe how can we use WEB API for Batch requests. Batch request is used to submit multiple requests/operations into a single HTTP request. POST request is used to submit a batch operation that contains multiple requests.

The batch request must have a Content-Type header value equal multipart/mixed plus a boundary equal the unique identifier of the batch.

Content-Type: multipart/mixed;boundary=batch_ABC123

Each items (POST, PATCH, PUT, DELETE, and GET) within the batch must be preceded by the batch identifier with a Content-Type and Content-Transfer-Encoding header like the following.

–batch_ABC123

Content-Type: application/httpContent-Transfer-Encoding:binary

The end of the batch must contain a termination indicator like the following:

— batch_ABC123–

Any operations that will change data must be included within a change set.

A batch request can include GET requests and change sets. Any operations that will change data must be included within a change set. GET requests must not be included in the change set.

When multiple operations are contained in a change set, and if any one of the operations fail, any completed operations will be rolled back.

Change sets must have a Content-Type header value equal to multipart/mixed with a boundary equal to the unique identifier of the change set, like shown below

–changeset_BBB456

Content-Type: application/httpContent-Transfer-Encoding:binary

The end of the change set must contain a termination indicator like the following:

–changeset_BBB456–  

WEB API  Bulk Create

The following example describe how to bulk create.


function BulkCreate() {
var body = "";
var entityCollection = new Array();

var entity1 = {};
entity1["name"] = "dummy account 1";
var body = JSON.stringify(entity1);
entityCollection.push(body);

body = "";
var entity2 = {};
entity2["name"] = "dummy account 2";
body = JSON.stringify(entity2);
entityCollection.push(body);

var data = [];
data.push('--batch_123456');
data.push('Content-Type: multipart/mixed;boundary=changeset_BBB456');
data.push('');

for (var i = 0; i < entityCollection.length; i++) {
data.push('--changeset_BBB456');
data.push('Content-Type:application/http');
data.push('Content-Transfer-Encoding:binary');
var id = i + 1;
data.push('Content-ID:' + id);
data.push('');
data.push('POST ' + parent.Xrm.Page.context.getClientUrl() + '/api/data/v8.1/accounts HTTP/1.1');

data.push('Content-Type:application/json;type=entry');
data.push('');
data.push(entityCollection[i]);
}

data.push('--changeset_BBB456--');
data.push('--batch_123456--');
var payload = data.join('\r\n');
$.ajax(
{
method: 'POST',
url: parent.Xrm.Page.context.getClientUrl() + '/api/data/v8.1/$batch',
headers: {
'Content-Type': 'multipart/mixed;boundary=batch_123456',
'Accept': 'application/json',
'Odata-MaxVersion': '4.0',
'Odata-Version': '4.0'
},
data: payload,
async: false,
success: function (data, textStatus, xhr) {
alert("Record has been successfully Created");

},
error: function (xhr, textStatus, errorThrown) {
alert(textStatus + " " + errorThrown);
}
});
}

WEB API  Bulk Update

Using the following example you can update entity using batch request


function BulkUpdate() {
var data = [];
data.push('--batch_123456');
data.push('Content-Type: multipart/mixed;boundary=changeset_BBB456');
data.push('');

//first request
data.push('--changeset_BBB456');
data.push('Content-Type:application/http');
data.push('Content-Transfer-Encoding:binary');
data.push('Content-ID:1');
data.push('');
data.push('PATCH ' + parent.Xrm.Page.context.getClientUrl() + '/api/data/v8.1/accounts(account guid to update) HTTP/1.1');
data.push('Content-Type:application/json;type=entry');
data.push('');
data.push('{ "name":"account name to updated" }');
//second request
data.push('--changeset_BBB456');
data.push('Content-Type:application/http');
data.push('Content-Transfer-Encoding:binary');
var id = i + 1;
data.push('Content-ID:2');
data.push('');
data.push('PATCH ' + parent.Xrm.Page.context.getClientUrl() + '/api/data/v8.1/accounts(account guid to update) HTTP/1.1');
data.push('Content-Type:application/json;type=entry');
data.push('');
data.push('{ "name":"account name to updated" }');
//end of changeset
data.push('--changeset_BBB456--');
//end of batch
data.push('--batch_123456--');
var payload = data.join('\r\n');

$.ajax(
{
method: 'POST',
url: parent.Xrm.Page.context.getClientUrl() + '/api/data/v8.1/$batch',
headers: {
'Content-Type': 'multipart/mixed;boundary=batch_123456',
'Accept': 'application/json',
'Odata-MaxVersion': '4.0',
'Odata-Version': '4.0'

},

data: payload,
async: false,
success: function (data, textStatus, xhr) {
alert("The record has been updated");

},
error: function (xhr, data, textStatus, errorThrown) {
alert(data, textStatus + " " + errorThrown);
}
});
}

WEB API Bulk Delete

Using the following example you can delete entity using batch request


function BulkDelete() {
var data = [];
data.push('--batch_123456');
data.push('Content-Type: multipart/mixed;boundary=changeset_BBB456');
data.push('');
//first delete request
data.push('--changeset_BBB456');
data.push('Content-Type:application/http');
data.push('Content-Transfer-Encoding:binary');
data.push('Content-ID:1');
data.push('');
data.push('DELETE ' + parent.Xrm.Page.context.getClientUrl() + '/api/data/v8.1/accounts(account guid to delete) HTTP/1.1');
data.push('Content-Type:application/json;type=entry');
data.push('');
data.push('{}');

//second delete request
data.push('--changeset_BBB456');
data.push('Content-Type:application/http');
data.push('Content-Transfer-Encoding:binary');
data.push('Content-ID:2');
data.push('');
data.push('DELETE ' + parent.Xrm.Page.context.getClientUrl() + '/api/data/v8.1/accounts(account guid to delete) HTTP/1.1');
data.push('Content-Type:application/json;type=entry');
data.push('');
data.push('{}');

//end of change set
data.push('--changeset_BBB456--');
//end of batch
data.push('--batch_123456--');
var payload = data.join('\r\n');
$.ajax(
{
method: 'POST',
url: parent.Xrm.Page.context.getClientUrl() + '/api/data/v8.1/$batch',
headers: {
'Content-Type': 'multipart/mixed;boundary=batch_123456',
'Accept': 'application/json',
'Odata-MaxVersion': '4.0',
'Odata-Version': '4.0'

},
data: payload,
async: false,
success: function (data, textStatus, xhr) {
alert("the records has been sucessfully deleted");

},
error: function (xhr, textStatus, errorThrown) {
alert(textStatus + " " + errorThrown);
}
});
}

WEB API  Bulk Retrieve

Using the following example you can retrieve records using batch request:


function BulkRetrieve() {
var data = [];
data.push('--batch_123456');
data.push("Content-Type: application/http");
data.push("Content-Transfer-Encoding:binary");
data.push('');
data.push('GET ' + Xrm.Page.context.getClientUrl() + '/api/data/v8.1/accounts(Record Id)?$select=name HTTP/1.1');
data.push('Content-Type:application/json;type=entry');
data.push('');
data.push('{}');

//second Retrieve request
data.push('--batch_123456');
data.push("Content-Type: application/http");
data.push("Content-Transfer-Encoding:binary");
data.push('');
data.push('GET ' + Xrm.Page.context.getClientUrl() + '/api/data/v8.1/accounts(AAA19CDD-88DF-E311-B8E5-6C3BE5A8B200)?$select=name HTTP/1.1');
data.push('Content-Type:application/json;type=entry');
data.push('');
data.push('{}');

//end of batch
data.push('--batch_123456--');
var payload = data.join('\r\n');
$.ajax(
{
method: 'POST',
url: parent.Xrm.Page.context.getClientUrl() + '/api/data/v8.1/$batch',
headers: {
'Content-Type': 'multipart/mixed;boundary=batch_123456',
'Accept': 'application/json',
'Odata-MaxVersion': '4.0',
'Odata-Version': '4.0'

},
data: payload,
async: false,
success: function (data, textStatus, xhr) {
alert(data);

},
error: function (xhr, textStatus, errorThrown) {
alert(textStatus + " " + errorThrown);
}
});
}

The response of retrieve request

WEB API Bulk Operations

This is all for now keep visiting our blog for the latest updates.

 



1 Comment

  • Rajeev Kumar

    Nicely put. Need help to understand the response of Batch Requests. The response is always 200 irrespective of it is a validation error in insert/update. Please suggest.

Leave a Reply