When managing data in SharePoint lists, there are times when we need to create multiple list items in one go. Doing this efficiently can save time and improve performance, especially when working with large datasets. Here, we’ll explore how to use the SharePoint REST API to create multiple list items at once, leveraging the power of batch requests.
Prerequisites
To follow along, you’ll need:
- A SharePoint Online site with at least one list where the items will be created.
- Access to SharePoint’s REST API, typically through an application page, client-side script, or a custom app.
- Basic knowledge of REST API and JavaScript (or another language of choice).
Understanding the SharePoint REST API Batch Processing
Batch processing allows you to group multiple requests in a single API call, which can help reduce the network overhead and increase performance when handling multiple items. SharePoint’s REST API allows batch processing by sending multiple requests in one HTTP POST
call. This is especially useful for creating, updating, or deleting multiple items.
Step-by-Step Guide
Step 1: Setting Up Authentication
For SharePoint Online, if you’re using client-side code, you can use JavaScript with an authentication token (typically managed with libraries like ADAL or MSAL). When working in SharePoint On-Premises, consider using the SharePoint REST API directly with NTLM authentication.
Step 2: Formulate the Batch Request Format
A batch request consists of a multi-part HTTP request, where each part represents an individual operation. Here’s how to structure it:
- Each part of the request is separated by a boundary identifier.
- Inside each part, define the HTTP method (e.g.,
POST
for creating an item). - Specify the metadata and fields for each item.
A basic batch request looks like this:
--batch_xyz123
Content-Type: application/http
Content-Transfer-Encoding: binary
POST https://yourtenant.sharepoint.com/sites/yoursite/_api/web/lists/getbytitle('YourList')/items
Accept: application/json;odata=verbose
Content-Type: application/json;odata=verbose
{
"__metadata": { "type": "SP.Data.YourListItem" },
"Title": "Item 1",
"Field1": "Value1",
"Field2": "Value2"
}
--batch_xyz123
Content-Type: application/http
Content-Transfer-Encoding: binary
POST https://yourtenant.sharepoint.com/sites/yoursite/_api/web/lists/getbytitle('YourList')/items
Accept: application/json;odata=verbose
Content-Type: application/json;odata=verbose
{
"__metadata": { "type": "SP.Data.YourListItem" },
"Title": "Item 2",
"Field1": "Value3",
"Field2": "Value4"
}
--batch_xyz123--
Step 3: Creating the Batch Request with JavaScript
Here’s a sample JavaScript code that prepares and sends the batch request:
async function createMultipleItems() {
const siteUrl = "https://yourtenant.sharepoint.com/sites/yoursite";
const listName = "YourList";
const batchBoundary = "batch_" + new Date().getTime();
// Prepare the batch request body
let batchBody = `
--${batchBoundary}
Content-Type: application/http
Content-Transfer-Encoding: binary
POST ${siteUrl}/_api/web/lists/getbytitle('${listName}')/items
Accept: application/json;odata=verbose
Content-Type: application/json;odata=verbose
{
"__metadata": { "type": "SP.Data.YourListItem" },
"Title": "Item 1",
"Field1": "Value1"
}
--${batchBoundary}
Content-Type: application/http
Content-Transfer-Encoding: binary
POST ${siteUrl}/_api/web/lists/getbytitle('${listName}')/items
Accept: application/json;odata=verbose
Content-Type: application/json;odata=verbose
{
"__metadata": { "type": "SP.Data.YourListItem" },
"Title": "Item 2",
"Field1": "Value2"
}
--${batchBoundary}--`;
// Define the batch request headers
const headers = {
"Content-Type": `multipart/mixed; boundary="${batchBoundary}"`,
"Accept": "application/json;odata=verbose"
};
// Send the batch request
const response = await fetch(`${siteUrl}/_api/$batch`, {
method: "POST",
headers: headers,
body: batchBody
});
if (response.ok) {
const result = await response.json();
console.log("Batch request succeeded:", result);
} else {
console.error("Batch request failed:", response.status, response.statusText);
}
}
createMultipleItems();
Explanation of the Code
- Batch Boundary: Each request is separated by a unique batch boundary.
- Content-Type: Specifies the type of each item in the list. The
__metadata
object within each item specifies the list type. - Fetching the API: We make a single API call with the
multipart/mixed
content type to handle the batch.
Handling the Response
The response will include details for each request. Inspect the result to confirm that all items were created successfully or handle any errors accordingly.
Advantages of Using Batch Requests
- Reduced Network Overhead: Batch requests minimize the number of HTTP calls, reducing the load on the network.
- Faster Execution: With fewer calls, SharePoint processes requests more quickly.
- Simplified Error Handling: Since responses come in a single object, it’s easier to manage and handle errors for multiple requests.
Using the SharePoint REST API’s batch feature, you can easily create multiple items in a single request. This approach is ideal for bulk data entry, migrations, or any scenario requiring batch data processing.
Try experimenting with different field values and item counts to see how this method can enhance your SharePoint solutions.
Automation Branding Collaboration Competitors Connect Content Type CSS Dates Design Flows Hillbilly Tabs Issues Javascript Limitation Limitations Luxon Microsoft Teams ModernScriptEditor NodeJs O365 Office 365 OneDrive Out Of The Box PnP Power Automate PowerShell Pwermissions Rest Endpoint ScriptEditor Send an HTTP Request to SharePoint SharePoint SharePoint Architecture SharePoint Designs SharePoint Modern SharePoint Online SharePoint Tabs ShellScript SPFX SPO Sync Teams Teams App TypeScript Versioning Workflows