在上一篇博客使用SPHttpClient对象与SharePoint交互中介绍了如何使用SPHttpClient对象调用SharePoint Rest API,这篇博客会继续介绍如何批量发送请求,减少请求的次数。
在SharePoint Framework 1.8.2正式版本中,SPHttpClient对象支持三个方法:
其中并没有批处理方法。但是在1.8.2的developer preview版本中支持批处理方法,如下所示:
所以在创建SPFx项目的时候,需要使用--plusbeta参数,创建preview版本:
yo @microsoft/sharepoint --plusbeta
在使用批处理方法之前需要导入相应的对象:
import {
SPHttpClient,
SPHttpClientResponse,
ISPHttpClientOptions,
SPHttpClientBatch,
ISPHttpClientBatchOptions,
ISPHttpClientBatchCreationOptions
} from '@microsoft/sp-http';
代码:
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);
const getListTitle: Promise<SPHttpClientResponse> = spBatch.get(`${currentWebUrl}/_api/web/lists/GetByTitle('MyTestList')/title`, SPHttpClientBatch.configurations.v1);
const batchOps: ISPHttpClientBatchOptions = {
body: `{ Title: 'Batch Test create'}`
};
const createItem: Promise<SPHttpClientResponse> = spBatch.post(`${currentWebUrl}/_api/web/lists/GetByTitle('MyTestList')/items`, SPHttpClientBatch.configurations.v1, batchOps);
spBatch.execute().then(() => {
getListTitle.then((response: SPHttpClientResponse) => {
response.json().then((webTitle: string) => {
console.log(webTitle);
});
});
createItem.then((response: SPHttpClientResponse) => {
response.json().then((responseJSON: any) => {
console.log(responseJSON);
});
});
});
返回值:
--batchresponse_7b273c4e-78b7-43ef-a438-0123631474f2
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 200 OK
CONTENT-TYPE: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8
{"@odata.context":"https://contoso.sharepoint.com/_api/$metadata#Edm.String","value":"MyTestList"}
--batchresponse_7b273c4e-78b7-43ef-a438-0123631474f2
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 201 Created
CONTENT-TYPE: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8
ETAG: "fdaa963f-983f-4be0-92ef-1a8ceddb087c,1"
LOCATION: https://contoso.sharepoint.com/_api/Web/Lists(guid'41e9dcd6-883e-406b-811b-5ea6138a9658')/Items(39)
{"@odata.context":"https://contoso.sharepoint.com/_api/$metadata#SP.ListData.MyTestListListItems/$entity","@odata.type":"#SP.Data.MyTestListListItem","@odata.id":"87343c81-4006-4171-9386-479cf89b5610","@odata.etag":"\"1\"","@odata.editLink":"Web/Lists(guid'41e9dcd6-883e-406b-811b-5ea6138a9658')/Items(39)","FileSystemObjectType":0,"Id":39,"ServerRedirectedEmbedUri":null,"ServerRedirectedEmbedUrl":"","ContentTypeId":"0x0100EFC90D7887CD3F4E8B3570770CB346DA","Title":"Batch Test create","OData__x0045_nd1":null,"Created":"2019-06-11T02:09:24Z","AuthorId":12,"EditorId":12,"OData__UIVersionString":"1.0","Attachments":false,"GUID":"2c1b19bc-4916-457d-86a9-d054d5b41859"}
--batchresponse_7b273c4e-78b7-43ef-a438-0123631474f2
Content-Type: application/http
Content-Transfer-Encoding: binary
--batchresponse_7b273c4e-78b7-43ef-a438-0123631474f2--