All the code for this, as always, is on GitHub: https://github.com/vman/SPFx-REST-Operations
This is one of my favorite things in the SharePoint Framework right now. The ability to easily batch REST API calls and send them to SharePoint in a single request.
There is a new class called SPHttpClientBatch which is used to combine multiple REST requests. You can find more about this in the API docs:
https://dev.office.com/sharepoint/reference/spdx/sp-http/sphttpclient
The get and post methods are syntactical sugar to call fetch but with the method parameter set to GET or POST.
The amazing thing about the fetch method is that it returns a Promise
So let's have a look at how we can utilize the SPHttpClientBatch class in an SPFx webpart:
First, we will need to import all the necessary modules:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { SPHttpClient, ISPHttpClientBatchOptions, ISPHttpClientBatchCreationOptions, SPHttpClientResponse, SPHttpClientBatch } from '@microsoft/sp-http'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private _makeSPHttpClientBatchRequest(): void { | |
// Here, 'this' refers to my SPFx webpart which inherits from the BaseClientSideWebPart class. | |
// Since I am calling this method from inside the class, I have access to 'this'. | |
const spHttpClient: SPHttpClient = this.context.spHttpClient; | |
const currentWebUrl: string = this.context.pageContext.web.absoluteUrl; | |
const spBatchCreationOpts: ISPHttpClientBatchCreationOptions = { webUrl: currentWebUrl }; | |
const spBatch: SPHttpClientBatch = spHttpClient.beginBatch(spBatchCreationOpts); | |
// Queue a request to get current user's userprofile properties | |
const getMyProperties: Promise<SPHttpClientResponse> = spBatch.get(`${currentWebUrl}/_api/SP.UserProfiles.PeopleManager/GetMyProperties`, SPHttpClientBatch.configurations.v1); | |
// Queue a request to get the title of the current web | |
const getWebTitle: Promise<SPHttpClientResponse> = spBatch.get(`${currentWebUrl}/_api/web/title`, SPHttpClientBatch.configurations.v1); | |
// Queue a request to create a list in the current web. | |
const currentTime: string = new Date().toString(); | |
const batchOps: ISPHttpClientBatchOptions = { | |
body: `{ Title: 'List created with SPFx batching at ${currentTime}', BaseTemplate: 100 }` | |
}; | |
const createList: Promise<SPHttpClientResponse> = spBatch.post(`${currentWebUrl}/_api/web/lists`, SPHttpClientBatch.configurations.v1, batchOps); | |
spBatch.execute().then(() => { | |
getMyProperties.then((response: SPHttpClientResponse) => { | |
response.json().then((props: any) => { | |
console.log(props); | |
}); | |
}); | |
getWebTitle.then((response: SPHttpClientResponse) => { | |
response.json().then((webTitle: string) => { | |
console.log(webTitle); | |
}); | |
}); | |
createList.then((response: SPHttpClientResponse) => { | |
response.json().then((responseJSON: any) => { | |
console.log(responseJSON); | |
}); | |
}); | |
}); | |
} |
When this code is executed, we can see that a single batch request is sent to SharePoint containing the information about the requests in the Payload:
(click to zoom)
And when the Response is returned, we can hook into each individual Promise object to get its value:
(click to zoom)
Isn't this Awesome? It is going to save a lot of round trips to the server and also help in performance optimization.
Thanks for reading!