Requirement
Save more than one record / multiple records to SharePoint 2013 list which is a part of App using REST API in a single request(Batch Processing).
Solution
This can be achieved using one of the following ways:
- Traditional Approach - SharePoint REST API ($batch)
- Simplified Approach - SharePoint REST API + BreezeJs
Note : To explain both these approaches, I am using SharePoint CustomList named "Employees" with 3 columns as "First Name","Last Name" and "Email.
For both these approaches SharePoint should receive data as shown below.
Traditional Approach
SharePoint provided $batch processing which requires below code snippet to generate data shown in above format. Below code is attached with this article as RESTAPI_Batch.js
var EmployeesAsJson = [
{
__metadata: {
type: 'SP.Data.EmployeesListItem'
},
FirstName: 'User1',
LastName: 'Test1',
Email: 'User1.Test1@email.com'
},
{
__metadata: {
type: 'SP.Data.EmployeesListItem'
},
FirstName: 'User2',
LastName: 'Test2',
Email: 'User2.Test2@email.com'
},
{
__metadata: {
type: 'SP.Data.EmployeesListItem'
},
FirstName: 'User3',
LastName: 'Test3',
Email: 'User3.Test3@email.com'
}
var batchGuid = generateUUID();
var batchContents = new Array();
var changeSetId = generateUUID();
var temp = document.createElement('a');
temp.href = _spPageContextInfo.webAbsoluteUrl;
var host = temp.hostname;
for (var employeeIndex = 0; employeeIndex < employeesAsJson.length; employeeIndex++) {
var employee = employeesAsJson[employeeIndex];
var endpoint = _spPageContextInfo.webAbsoluteUrl
+ '/_api/web/lists/getbytitle(\'Employees\')'
+ '/items';
batchContents.push('--changeset_' + changeSetId);
batchContents.push('Content-Type: application/http');
batchContents.push('Content-Transfer-Encoding: binary');
batchContents.push('');
batchContents.push('POST ' + endpoint + ' HTTP/1.1');
batchContents.push('Content-Type: application/json;odata=verbose');
batchContents.push('');
batchContents.push(JSON.stringify(employee));
batchContents.push('');
}
batchContents.push('--changeset_' + changeSetId + '--');
var batchBody = batchContents.join('\r\n');
var endpoint = _spPageContextInfo.webAbsoluteUrl
+ '/_api/$batch';
var batchRequestHeader = {
'X-RequestDigest': jQuery("#__REQUESTDIGEST").val(),
'Content-Type': 'multipart/mixed; boundary="batch_' + batchGuid + '"'
};
jQuery.ajax({
url: endpoint,
type: 'POST',
headers: batchRequestHeader,
data: batchBody,
success: function (response) {
alert("Successfully saved a batch request");
},
fail: function (error) {
alert('.. create employee FAIL ', error);
}
});
Simplified Approach
Coding of above content needs an lot amount of time and patience. As an alternate, Breezejs can be used to simplify the formatting and submission of data. We need to reference Breeze libraries in SharePoint App Page. Below is the reference for that.
<!-- breeze & dependent libraries -->
<script type="text/javascript" src="../Scripts/q.js"></script>
<script type="text/javascript" src="../Scripts/breeze.debug.js"></script>
<script type="text/javascript" src="../Scripts/breeze.labs.dataservice.abstractrest.js"></script>
<script type="text/javascript" src="../Scripts/breeze.labs.dataservice.sharepoint.js"></script>
<script type="text/javascript" src="../Scripts/breeze.metadata-helper.js"></script>
Here is the code format while using breeze libraries. Below code is available with this article RESTAPI_Breeze.js
function createItem() {
var Employee1 = entityManager.createEntity(EmployeeType);
Employee1.FirstName = 'User1';
Employee1.Title = 'Test1';
Employee1.Email = 'User1.Test1@email.com';
var Employee2 = entityManager.createEntity(EmployeeType);
Employee2.FirstName = 'User2';
Employee2.Title = 'Test2';
Employee2.Email = 'User2.Test2@email.com';
var Employee3 = entityManager.createEntity(EmployeeType);
Employee3.FirstName = 'User3';
Employee3.Title = 'Test3';
Employee3.Email = 'User3.Test2@email.com';
entityManager.saveChanges()
.then(function () {
alert("Successfully saved a batch request");
});
}
Upcoming Articles
I am going to write new article(s) for the following content:
- Configuring Breeze to SharePoint 2013 App
- Example of breeze along with code for Create,Update,Delete operations.