SharePoint Framework Development - Create Client Webpart to Retrieve and Display List Items

Published: 9 June 2017

This is the continuation of the SharePoint Framework Development series, In the earlier article we saw how to set up the environment and the tool chains required to start the development using SharePoint Framework(SPFx). Lets create a client web part using TypeScript and SPFx which will be retrieving the list items from SharePoint List (EmployeeList) and will display it in the tabular form in the client Web part, as shown below.

Create the Web part project

Before moving forward, ensure that the SharePoint Framework development environment is ready. Spin up Node.js command prompt using which we will be creating the web part project structure

We can create the directory, where we will be adding the solution, using the command given below.

md GetSharePointListItems 

Let’s move to the newly created working directory, using the command.

cd GetSharePointListItems

We will then create the client web part by running the Yeoman SharePoint Generator:

yo @microsoft/sharepoint

 This will display the prompt, which we must fill up, to proceed with the project creation.

●      What is your solution name? : Set it to ‘GetSPListItems’.

On pressing enter, we will be asked to chose the working folder for the project.

 ●      Where do you want to place your files- Use current folder.

●      What framework would you like to start with- Select “No javaScript web framework” for the time being, as this is a sample Web part.

●      What is your Webpart name- We will specify it as ‘GetSPListItems’ and press Enter

●      What is your Webpart description- We will specify it as this Webpart will retrieve the list items from SharePoint list and display in a table

Yeoman has started working on the scaffolding of the project. It will install the required dependencies and scaffold the solution files for the ‘GetListItems’ Web part, which will take some time to complete. Once completed, we will get a congratulations message.

Test the web part locally

To test the client Web part, we can build and run it on the local Web Server, where we are developing the Web part. SharePoint Framework development uses HTTPS endpoint by default. Since a default certificate is not configured for the local development environment, our Browser will report a certificate error. SharePoint Framework tool chain comes with a developer certificate, which we can install for testing the client Web parts locally. From the current Web part directory, run the command given below. 

gulp trust-dev-cert

Now, let’s preview the Web part by running the gulp serve command.

This command will execute a series of gulp tasks and will create a Node-based HTTPS Server at 'localhost:4321'. It will then open the Browser and display the client Web part.

This indicates that the project structure is set up correctly. We will now open the solution in Visual Studio Code to add the logic to retrieve the list items from SharePoint and display it as a table in this page.

To stop Gulp from listening to the process, we can press ‘Control + C’. This will terminate the Gulp Serve command and stop the Server.

Edit the Webpart

Now let’s try to edit the web part and add more functionality to it. To do that navigate to “src\webparts\getSpListItems” location.

In the left pane of Visual Studio Code, we can see the project structure. The bulk of the logic resides within the GetSPListItemsWebPart.ts file. Let’s add the code to retrieve SharePoint list items from the Employee List within this TypeScript file.

Define List Model

Since we want to retrieve an Employee list items data, we will be creating list model with SharePoint list fields in the GetSpListItemsWebPart.TS file, as shown below. Place it above the ‘GetSpListItemsWebPart’ class. 

Create Mock HTTPClient to test data locally

In order to test the list item retrieval in the local workbench, we will create a mock store, which returns mock Employee list data. We will create a new file inside ‘src\webparts\ getSpListItems’ folder named MockHttpClient.ts, as shown below. This is something that is optional. However to check the functionality locally without connecting to SharePoint, we can make use of the mock client. 

We will then copy the code given below into MockHttpClient.ts, as shown below. 

We can now use the MockHttpClient class in the ‘GetSPListItems’ class. Let’s import the ‘MockHttpClient’ module by going to the GetSpLitItemsWebPart.ts and pasting the line given below just after “import { IGetSpListItemsWebPartProps } from './IGetSpListItemsWebPartProps';” 

import MockHttpClient from './MockHttpClient';

We will also add the mock list item retrieval method within the ‘GetSpListItemsWebPart’ class. 

Retrieve SharePoint List Items

SharePoint Framework has the helper class spHttpClient, which can be utilized to call REST API requests against SharePoint. We will use REST API: “/_api/web/lists/GetByTitle('EmployeeList')/Items” to get the list items from SharePoint List.

To use ‘spHttpClient’, we will first have to import it from the ‘@microsoft/sp-http’ module. We will import this module by placing the line given below after the mockHttpClient import code -“import MockHttpClient from './MockHttpClient';” 

import {   SPHttpClient } from '@microsoft/sp-http';

 We will be then adding the method given below to get SharePoint list items, using REST API within the ‘GetSpListItemsWebPart’ class. 

Render the SharePoint List Items From Employee List

Once we run the gulp serve command, we can test the Web part in SharePoint Workbench in the local environment or using SharePoint Online Context. SharePoint Framework uses ‘EnvironmentType’ module to identify the environment, where the Web part is executed.

In order to implement this, we will import ‘Environment’ and the ‘EnvironmentType’ modules from the @microsoft/sp-core-library bundle by placing it at the top of the GetSpListItemsWebpart.ts file. 

import {   Environment,   EnvironmentType } from '@microsoft/sp-core-library';

We will then check Environment.type value and if it is equal to Environment.Local, the MockHttpClient method, which returns dummy data will be called else the method that calls REST API to retrieve SharePoint list items will be called. 

Finally, we will add the method given below, which will create HTML table out of the retrieved SharePoint list items. 

To enable rendering of the list items given above, we will replace Render method in the ‘GetSpListItemsWebPart’ class with the code given below. 

Test the Web part in local SharePoint Workbench

Now,  we can see the output generated in the local SharePoint Workbench.

Since the environment is local, the mock data has been used to generate the table, as shown below.

Thus, we have successfully tested the client Web part locally.

Test the Web part in SharePoint Online

Now, let’s test the Web part in SharePoint Workbench available in SharePoint Online. This time, the 'EnvironmentType' check will evaluate to SharePoint and REST API endpoint method will be called to retrieve the list items from SharePoint list. SharePoint Online list EmployeesList to which we are trying to connect, using REST API is given below.

Once we have login in to SharePoint Online, we can invoke the workbench by appending the text ‘_layouts/15/workbench.aspx’ to SharePoint Online site collection URL. As we can see below, the items have been successfully retrieved, using REST API and the data has been built into HTML table in the client Web part.

We can further modify the CSS by making changes in the ‘GetSpListItems.module.scss’ file.

The main TS file contents are shown below .You can download the main solution files used for the demo from here. To sum up, the control flow would be :

  • render() is invoked which builds the basic UI and calls the _renderListAsync() .
  • _renderListAsync() method checks if the environment is local or SharePoint Online.
  • If it is local environment, _getMockListData() is invoked and the mock data is retrieved. Else the _getListData() is invoked and SharePoint data is retrieved using REST api. 
  • Finally the retrieved data is passed to _renderList to create the table at the predefined div named spListContainer


Thus we saw how to retrieve and display SharePoint List Items using SharePoint Framework. In the next post we will see how to use SharePoint Framework to provision site assets like List and deploy it to SharePoint Online Site.


About Author

Priyaranjan is a SharePoint Consultant with 7 years experience in developing and deploying SharePoint Applications.He has worked on various SharePoint iterations starting from MOSS 2007 through SharePoint 2016 and Office 365. He is a frequent contributor at Microsoft TechNet and has won 25 Gold Medals in various TechNet Wiki Guru Monthly Competitions.As a token of appreciation for the TechNet community activities, he was interviewed by Microsoft Program Manager,Ed Price. He has also published 300 Articles and 4 SharePoint Ebooks in different technical communities.You can find his Microsoft TechNet contributions here

If you found this post helpful, please share with your friends!!

Join 6,000 subscribers and receive instant access to

Devendra's Library of Resources for SharePoint Developers