SharePoint List Creation Using PnP JS And SPFx

We can seamlessly integrate PnP JS files 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

In this section, we will see how to use PnP JS in SPFx to create and provision a custom list. The major project files used in this solution has been zipped and uploaded at Microsoft TechNet Gallery. Feel free to download it.

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

md CreatePnPList

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

cd CreatePnPList

SharePoint

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

yo @microsoft/sharepoint

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 ‘CreatePnPList’.

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 ‘CreatePnPList’ and press Enter
  • What is your Webpart description- We will specify it as ‘List created using PnP JS and SharePoint Framework’.

    SharePoint

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

Edit the web part

Run ‘Code .’ to open the project in Visual Studio Code.

SharePoint

Install PnP JS Module

Now, we must load PnP JS file which we will use within the project to create list. We will be using npm to add PnP JS file.

npm install sp-pnp-js --save

SharePoint

Thus, PnP JS has been loaded to the project. We can refer it in the project by using

import { Web } from "sp-pnp-js";

SharePoint

Create List using PnP method

We can create the list using PnP js method - spWeb.lists.add, as shown below. We will be creating a custom list named SPFxPnpList which has the template id : 100.

  1. private CreateList(): void {  
  2.     let spWeb = new Web(this.context.pageContext.web.absoluteUrl);  
  3.     let spListTitle = "SPFxPnPList";  
  4.     let spListDescription = "SPFxPnP List";  
  5.     let spListTemplateId = 100;  
  6.     let spEnableCT = false;  
  7.     lists.add(spListTitle, spListDescription, spListTemplateId, spEnableCT).then(function(splist) {  
  8.         getElementById("ListCreationStatus").innerHTML += `New List ` + spListTitle + ` Created`;  
  9.     });  
  10. }   

Once the web part has been created, the status will be updated in the div.

  1. <div id="ListCreationStatus" />  

TS File code for Creating the List

The entire code for the TS file is shown below. The ‘this.CreateList()’ method in the render method will call the PnP list creation method and create the SharePoint list.

  1. import {  
  2.     Web  
  3. } from "sp-pnp-js";  
  4. import {  
  5.     Version  
  6. } from '@microsoft/sp-core-library';  
  7. import {  
  8.     BaseClientSideWebPart,  
  9.     IPropertyPaneConfiguration,  
  10.     PropertyPaneTextField  
  11. } from '@microsoft/sp-webpart-base';  
  12. import {  
  13.     escape  
  14. } from '@microsoft/sp-lodash-subset';  
  15. import styles from './CreatePnPList.module.scss';  
  16. import * as strings from 'createPnPListStrings';  
  17. import {  
  18.     ICreatePnPListWebPartProps  
  19. } from './ICreatePnPListWebPartProps';  
  20. export default class CreatePnPListWebPart extends BaseClientSideWebPart < ICreatePnPListWebPartProps > {  
  21.     private CreateList(): void {  
  22.         let spWeb = new Web(this.context.pageContext.web.absoluteUrl);  
  23.         let spListTitle = "SPFxPnPList";  
  24.         let spListDescription = "SPFxPnP List";  
  25.         let spListTemplateId = 100;  
  26.         let spEnableCT = false;  
  27.         lists.add(spListTitle, spListDescription, spListTemplateId, spEnableCT).then(function(splist) {  
  28.             getElementById("ListCreationStatus").innerHTML += `New List ` + spListTitle + ` Created`;  
  29.         });  
  30.     }  
  31.     public render(): void {  
  32.         domElement.innerHTML = `  
  33. <div class="${styles.helloWorld}">  
  34. <div class="${styles.container}">  
  35. <div class="ms-Grid-row ms-bgColor-themeDark ms-fontColor-white ${styles.row}">  
  36. <div class="ms-Grid-col ms-u-lg10 ms-u-xl8 ms-u-xlPush2 ms-u-lgPush1">  
  37. <span class="ms-font-xl ms-fontColor-white" style="font-size:28px">Welcome to SharePoint Framework Development using PnP JS Library</span>  
  38. <p class="ms-font-l ms-fontColor-white" style="text-align: left">Demo : Create SharePoint List</p>  
  39. </div>  
  40. </div>  
  41. <div class="ms-Grid-row ms-bgColor-themeDark ms-fontColor-white ${styles.row}">  
  42. <div style="background-color:Black;color:white;text-align: center;font-weight: bold;font-size:18px;">Employee Details</div>  
  43. <br>  
  44. <div id="ListCreationStatus" />  
  45. </div>  
  46. </div>  
  47. </div>`;  
  48.         CreateList();  
  49.     }  
  50.     protected get dataVersion(): Version {  
  51.         return Version.parse('1.0');  
  52.     }  
  53.     protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {  
  54.         return {  
  55.             pages: [{  
  56.                 header: {  
  57.                     description: strings.PropertyPaneDescription  
  58.                 },  
  59.                 groups: [{  
  60.                     groupName: strings.BasicGroupName,  
  61.                     groupFields: [  
  62.                         PropertyPaneTextField('description', {  
  63.                             label: strings.DescriptionFieldLabel  
  64.                         })  
  65.                     ]  
  66.                 }]  
  67.             }]  
  68.         };  
  69.     }  
  70. }   

Run gulp serve to package the solution.

SharePoint

Test the Web part in SharePoint Online

Now, let’s test the Web part in SharePoint Workbench available in SharePoint Online. 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. Add the webpart to the page by selecting CreatePnPList icon.

SharePoint

This will deploy the list to SharePoint and will show a success message in the UI as shown below.

SharePoint

Heading over to site contents, we can see the newly created list.

SharePoint

SharePoint


The major project files used in this solution has been zipped and uploaded at Microsoft TechNet Gallery. Feel free to download it.

Summary

Thus, we saw how to create SharePoint List using SharePoint Framework and PnP JS.

Up Next
    Ebook Download
    View all
    Learn
    View all