How To Run Batch API In SharePoint With Only One HTTP Call

In this blog, I will show how to update multiple listitem in SharePoint Online List/Library using single HTTP call.

We need the site url where we will be running the batch API. Let's say https://contoso.sharepoint.com/sites/{SiteName} is my siteurl where my list resides.

I will then need 2 GUID for ChangeSet and Batch which will be used as below,

Here Changeset is used when we have more than one operation within them. And if any of the child operations fails, the others still complete and aren't rolled back as the API in SharePoint are not transactional. And Batch is something that contains Multiple API of Changeset.

Example

1)  In below sample I have shown to update multiple SitePage item using single HTTP call.

POST https://contoso.sharepoint.com/sites/{SiteName}

X-RequestDigest: digest
Content-Type: multipart/mixed; boundary=batch_2226b7dd-8d6b-4f85-85e3-529e98fdd109

BODY

--batch_2226b7dd-8d6b-4f85-85e3-529e98fdd109
Content-Type: multipart/mixed; boundary="changeset_c10cf26f-de12-4a4c-9c19-3ecce382386e"
Content-Transfer-Encoding: binary

--changeset_c10cf26f-de12-4a4c-9c19-3ecce382386e
Content-Type: application/http
Content-Transfer-Encoding: binary

PATCH https://contoso.sharepoint.com/sites/{SiteName}/_api/web/lists/getByTitle('Site Pages')/items('1') HTTP/1.1
Content-Type: application/json;odata=verbose
If-Match: *

{"__metadata":{"type":"SP.Data.SitePagesListItem"},"ID":"1","ViewCount":10,"Description": "This is Description for test page 1"}

--changeset_c10cf26f-de12-4a4c-9c19-3ecce382386e
Content-Type: application/http
Content-Transfer-Encoding: binary

PATCH https://contoso.sharepoint.com/sites/{SiteName}/_api/web/lists/getByTitle('Site Pages')/items('2') HTTP/1.1
Content-Type: application/json;odata=verbose
If-Match: *

{"__metadata":{"type":"SP.Data.SitePagesListItem"},"ID":"2","Description":"This is the Description for test page 2"}

--changeset_c10cf26f-de12-4a4c-9c19-3ecce382386e--

--batch_2226b7dd-8d6b-4f85-85e3-529e98fdd109--

2) Sample to fetch the comments for 2 or more SitePages.

Note
Here, even if the query is for GET still we need to use POST method here.

POST https://contoso.sharepoint.com/sites/{SiteName}

Accept: application/json;odata=nometadata
Content-Type: multipart/mixed; boundary=batch_68a3ac74-a065-4e4d-8c61-4b74f3fb3aea

BODY

--batch_68a3ac74-a065-4e4d-8c61-4b74f3fb3aea
Content-Type: application/http
Content-Transfer-Encoding: binary

GET https://contoso.sharepoint.com/sites/{SiteName}/_api/web/lists/getByTitle('Site Pages')/items('1')/Comments?$expand=replies HTTP/1.1
Accept: application/json;odata=nometadata

--batch_68a3ac74-a065-4e4d-8c61-4b74f3fb3aea
Content-Type: application/http
Content-Transfer-Encoding: binary

GET https://contoso.sharepoint.com/sites/{SiteName}/_api/web/lists/getByTitle('Site Pages')/items('2')/Comments?$expand=replies HTTP/1.1
Accept: application/json;odata=nometadata

--batch_68a3ac74-a065-4e4d-8c61-4b74f3fb3aea--

Note
In the above sample, I have used the same List or Library it is not mandatory to use same list or library, even with different list or library it will work. It just needs to be in the same site.