SharePoint Framework is a powerful client-side development tool used to develop SharePoint apps with the full support of most of the other JavaScript frameworks like Angular, React, Knockout…
Generally, in order to access the SharePoint REST API call request, we would be in need of authentication parameters like access tokens, Request Digest value, etc.
But in SPFx, we have no need to worry about that because SPFx takes care of that authentication by using the current user’s context in SPHttpClient class. The only thing we need to do is to pass the URL with some headers and post body.
We can accomplish all the list operations by using the “Get” and “Post” methods with the below options.
- url
SP Rest API url to access your data
- configuration
SPHttpClientConfiguration which uses predefined objects required for SPHttpClient class
- options
ISPHttpClientOptions which is optional in the GET request and required in POST request. With this parameter, we can pass custom headers and payload to post.
Let’s see real examples to retrieve data from the list with the help of spHttpClientClass. Below are the basic request URLs required to read list data.
- GetListItemCount
To get the count of the items in a list“/_api/web/Lists/GetByTitle('YourListTitle')/ItemCount” - GetListItems
To get the count of the items in a list
Here OData query is used to read the attachment files in the list in a single call while reading the list items./_api/web/Lists/GetByTitle('YourListTitle')/items$select=Attachments,AttachmentFiles,*&$expand=AttachmentFiles” - GetListItemsWithCAMLQuery
To get the count of the items in a list/_api/web/Lists/GetByTitle('YourListTitle')/GetItems$select=Attachments,AttachmentFiles,*&$expand=AttachmentFiles” - GetListItemByItemId
To get the count of the items in a list/_api/web/Lists/GetByTitle('YourListTitle')/items(YourItemID)$select=Attachments,AttachmentFiles,*&$expand=AttachmentFiles”
The first step is to import the below modules in your TypeScript file from the package “@microsoft/sp-http” like below.
import {
SPHttpClient, SPHttpClientResponse, ISPHttpClientOptions} from '@microsoft/sp-http';
Next, to get the web absolute URL where the SPFx app is running -
let currentWebUrl = this.context.pageContext.web.absoluteUrl;
Get Items count in a list (Get operation is used)
let requestUrl = currentWebUrl.concat(“/_api/web/Lists/GetByTitle('YourListTitle')/ItemCount”)
this.context.spHttpClient.get(requestUrl, SPHttpClient.configurations.v1)
.then((response: SPHttpClientResponse) => {
if (response.ok) {
response.json().then((responseJSON) => {
if (responseJSON!=null && responseJSON.value!=null){
let itemCount:number = parseInt(responseJSON.value.toString());
}
});
}
}); 
Abu ZermatPosted Feb 28, 2024, 4:39 PM
Hi Jayakumar Balasubramaniam any update regarding about how to add/update/delete items from a list?
Nagarajan ElumalaiPosted Aug 23, 2018, 1:06 AM
No its not a custom master page. I am editing the public render(): void { this.domElement.innerHTML = in this section am adding div and multiple table. Here i have one more question. div class="${styles.header}" style="text-align: center. example consider the above div. When am typing this html tag in visual code environment am not getting css intellisense. Especially when i type something on style=" it not bringing anything. Do we have any option to bring intellisense here.
Nagarajan ElumalaiPosted Aug 22, 2018, 12:43 AM
Hi Jayakumar am getting error while creating a webpart. Below i have given the details. Could you please assist me on it. Hi, I am creating a SPFx webpart. I added few div and some buttons using html tag inside domelement.innerhtml. I try to run the webpart using gulp serve. I am getting the below error. **10:41:19] Error - [webpack] 'dist': ./lib/webparts/issueManagement/IssueManagementWebPart.js Module not found: Error: Can't resolve 'clientforms' in Parsed request is a module using description file: C:\Users\nagarajan\Issue\package.json (relative path: ./lib/webparts/issue) Field 'browser' doesn't contain a valid alias configuration** Please assist me what should i do on this.
Dheepa IyerPosted Aug 1, 2018, 7:19 PM
Hello, I am getting an error when using your code sample for CAML query. Any thoughts? TypeError: Failed to construct 'Request': Request with GET/HEAD method cannot have body.