The SharePoint Framework Release Candidate 0 (RC0) was released recently which had some breaking changes compared to the previous drops. You can see the release notes here: https://github.com/SharePoint/sp-dev-docs/wiki/Release-Notes-RC0
One of these changes was how REST requests are handled when talking to SharePoint. Lets have a look at what changed and also see some code samples I created when tinkering around with the latest changes.
Before that, you should know that I have also updated my previous SPFx REST API related posts for RC0. If you are interested, you can have a look here:
Batch REST requests in SharePoint Framework (SPFx) using SPHttpClientBatch
Making a POST request to SharePoint from an SPFx webpart
The HttpContext and all REST API related operations are now part of the @microsoft/sp-http package.
There is a new class SPHttpClient which has inbuilt get and post methods which make it really easy to query SharePoint. The methods automatically add headers such as ODataVersion and Reqest Digest which are needed when making a REST call to SharePoint.
The SPHttpClientConfiguration class can be used to either let default headers be set on the REST request or it also allows you to override certain headers if needed. Lets have a look at this:
Making GET requests to SharePoint is really easy when you specify the SPHttpClientConfiguration as SPHttpClientConfigurations.v1. It sets the following headers to your REST request. You don't have to set any headers manually.
consoleLogging = true;
jsonRequest = true;
jsonResponse = true;
defaultSameOriginCredentials = true;
defaultODataVersion = ODataVersion.v4;
requestDigest = true
First, you will need to import the required modules from the @microsoft/sp-http package as mentioned earlier:
Then use this code to make a GET request to SharePoint. The following code makes 3 different requests to SharePoint. The first one gets the current web, the second one gets the current user from the User Information List and the third one gets the current user properties from the User Profile Service:
For making a POST request, you have to use the SPHttpClient.post method. I have previously blogged about this for the earlier SPFx drops. I have now updated the same post for RC0. Check it out here: http://www.vrdmn.com/2016/08/making-post-request-to-sharepoint-from.html
This is a very interesting change for RC0. With the earlier drops, the default OData Version attached with each SPFx REST request was 4. This caused the calls to the SharePoint Search REST Api to fail and return a "500 Internal Server Error" as the search API would only work with Odata Version 3.0.
The issue is discussed on GitHub SPFx repo here: https://github.com/SharePoint/sp-dev-docs/issues/44
As a result of that, SPFx now provides you a way to override the default headers sent with the REST request:
Thanks for reading! All the code for this, as always, is on GitHub: https://github.com/vman/SPFx-REST-Operations
One of these changes was how REST requests are handled when talking to SharePoint. Lets have a look at what changed and also see some code samples I created when tinkering around with the latest changes.
Before that, you should know that I have also updated my previous SPFx REST API related posts for RC0. If you are interested, you can have a look here:
Batch REST requests in SharePoint Framework (SPFx) using SPHttpClientBatch
Making a POST request to SharePoint from an SPFx webpart
1) SPHttpClient, SPHttpClientConfiguration and the '@microsoft/sp-http' package
The HttpContext and all REST API related operations are now part of the @microsoft/sp-http package.
There is a new class SPHttpClient which has inbuilt get and post methods which make it really easy to query SharePoint. The methods automatically add headers such as ODataVersion and Reqest Digest which are needed when making a REST call to SharePoint.
The SPHttpClientConfiguration class can be used to either let default headers be set on the REST request or it also allows you to override certain headers if needed. Lets have a look at this:
2) GET requests
Making GET requests to SharePoint is really easy when you specify the SPHttpClientConfiguration as SPHttpClientConfigurations.v1. It sets the following headers to your REST request. You don't have to set any headers manually.
consoleLogging = true;
jsonRequest = true;
jsonResponse = true;
defaultSameOriginCredentials = true;
defaultODataVersion = ODataVersion.v4;
requestDigest = true
First, you will need to import the required modules from the @microsoft/sp-http package as mentioned earlier:
Then use this code to make a GET request to SharePoint. The following code makes 3 different requests to SharePoint. The first one gets the current web, the second one gets the current user from the User Information List and the third one gets the current user properties from the User Profile Service:
3) POST requests
For making a POST request, you have to use the SPHttpClient.post method. I have previously blogged about this for the earlier SPFx drops. I have now updated the same post for RC0. Check it out here: http://www.vrdmn.com/2016/08/making-post-request-to-sharepoint-from.html
4) Calling SharePoint Search REST API with OData Version 3
This is a very interesting change for RC0. With the earlier drops, the default OData Version attached with each SPFx REST request was 4. This caused the calls to the SharePoint Search REST Api to fail and return a "500 Internal Server Error" as the search API would only work with Odata Version 3.0.
The issue is discussed on GitHub SPFx repo here: https://github.com/SharePoint/sp-dev-docs/issues/44
As a result of that, SPFx now provides you a way to override the default headers sent with the REST request:
Thanks for reading! All the code for this, as always, is on GitHub: https://github.com/vman/SPFx-REST-Operations