SharePoint Framework Development Using PnP JS


Published: 14 July 2017

 We can seamlessly integrate PnP JS file with SharePoint. The new Patterns and Practices JavaScript Core Library was created to help developers by simplifying common operations within SharePoint. Currently it contains a fluent API for working with the full SharePoint REST API as well as utility and helper functions. We can use them with SharePoint Framework to work with SharePoint with minimal effort. You can get detailed information and documentation about PnP JS from here

This is a continuation post in the SharePoint Framework Development series, In a previous article, we saw how to set up the development environment for SharePoint Framework. We also saw the basic Hello World web part creation and  how to provision SharePoint List with custom Site Columns and Content Type here

In this article we will see how to retrieve SharePoint List Items using SharePoint Framework and PnP JS.The major code files used for the demo can be downloaded from here. The output of the SPFx webpart implemented using PnP is shown below :

Retrieve SharePoint List Items using PnP JS and SharePoint Framework

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

md PnPSPFrameworkGetItems 

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

cd PnPSPFrameworkGetItems

We will then create the client Web part by running the Yeoman SharePoint Generator.

yo @microsoft/sharepoint

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

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

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 ‘PnPSPFrameworkGetItems ‘and press Enter

●      What is your Webpart description- We will specify it as ‘Get SP List items using PnP’

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

Edit web part

Run Code . to open the project in Visual Studio Code

Now we have to load PnP JS file which we will use within the project to create list.We will be using npm to add PnP JS file as shown below. 

npm install sp-pnp-js --save

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 PnPspFrameworkGetItems.TS file, as shown below. Place it above the ‘PnPspFrameworkGetItems’ 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 PnPspFrameworkGetItemsWebPart’ folder named MockHttpClient.ts. 

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

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

 import MockHttpClient from './MockHttpClient';

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

Retrieve SharePoint List Items

We will be making use of PnP library to get the list items as shown below:

Retrieve 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. 

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 PnPspFrameworkGetItemsWebpart.ts file. 

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 PnP JS method that will 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 ‘PnPspFrameworkGetItemsWebPart’ class with the code given below.

TS File Contents to retrieve list data using PnP

The entire TS file contents that was used to implement data retrieval in the SPFx webpart is given below:

Package and Deploy the Solution

Let’s run Gulp serve to package the solution and test it in the workbench.

Test the Web part in local SharePoint Workbench

We can see the output generated in the local SharePoint Workbench.

Let’s add the web part by clicking on the Plus sign.

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

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 the PnP method will be called to retrieve the list items from SharePoint list. Once we have logged in to SharePoint Online, we can invoke the workbench by appending the text ‘_layouts/15/workbench.aspx’ to SharePoint Online URL. 

It has picked up the list items from the list and displayed it as a table.

Summary

Thus, we saw how to retrieve SharePoint List Items using SharePoint Framework and PnP JS.In the upcoming article we will see how to work with Search and User Profiles using PnP and SPFx. 

Download the guide to develop client webpart using SharePoint Framework.

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 30 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 it with your friends!!


Join 6,000 subscribers and receive instant access to

Devendra's Library of Resources for SharePoint Developers



-- FREE Workshop --

Modern sites and SharePoint Framework

ENROLL TODAY