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
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 ‘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’.
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.
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
Thus, PnP JS has been loaded to the project. We can refer it in the project by using
import { Web } from "sp-pnp-js";
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.
- private CreateList(): void {
- let spWeb = new Web(this.context.pageContext.web.absoluteUrl);
- let spListTitle = "SPFxPnPList";
- let spListDescription = "SPFxPnP List";
- let spListTemplateId = 100;
- let spEnableCT = false;
- lists.add(spListTitle, spListDescription, spListTemplateId, spEnableCT).then(function(splist) {
- getElementById("ListCreationStatus").innerHTML += `New List ` + spListTitle + ` Created`;
- });
- }
Once the web part has been created, the status will be updated in the div.
- <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.
- import {
- Web
- } from "sp-pnp-js";
- import {
- Version
- } from '@microsoft/sp-core-library';
- import {
- BaseClientSideWebPart,
- IPropertyPaneConfiguration,
- PropertyPaneTextField
- } from '@microsoft/sp-webpart-base';
- import {
- escape
- } from '@microsoft/sp-lodash-subset';
- import styles from './CreatePnPList.module.scss';
- import * as strings from 'createPnPListStrings';
- import {
- ICreatePnPListWebPartProps
- } from './ICreatePnPListWebPartProps';
- export default class CreatePnPListWebPart extends BaseClientSideWebPart < ICreatePnPListWebPartProps > {
- private CreateList(): void {
- let spWeb = new Web(this.context.pageContext.web.absoluteUrl);
- let spListTitle = "SPFxPnPList";
- let spListDescription = "SPFxPnP List";
- let spListTemplateId = 100;
- let spEnableCT = false;
- lists.add(spListTitle, spListDescription, spListTemplateId, spEnableCT).then(function(splist) {
- getElementById("ListCreationStatus").innerHTML += `New List ` + spListTitle + ` Created`;
- });
- }
- public render(): void {
- domElement.innerHTML = `
- <div class="${styles.helloWorld}">
- <div class="${styles.container}">
- <div class="ms-Grid-row ms-bgColor-themeDark ms-fontColor-white ${styles.row}">
- <div class="ms-Grid-col ms-u-lg10 ms-u-xl8 ms-u-xlPush2 ms-u-lgPush1">
- <span class="ms-font-xl ms-fontColor-white" style="font-size:28px">Welcome to SharePoint Framework Development using PnP JS Library</span>
- <p class="ms-font-l ms-fontColor-white" style="text-align: left">Demo : Create SharePoint List</p>
- </div>
- </div>
- <div class="ms-Grid-row ms-bgColor-themeDark ms-fontColor-white ${styles.row}">
- <div style="background-color:Black;color:white;text-align: center;font-weight: bold;font-size:18px;">Employee Details</div>
- <br>
- <div id="ListCreationStatus" />
- </div>
- </div>
- </div>`;
- CreateList();
- }
- protected get dataVersion(): Version {
- return Version.parse('1.0');
- }
- protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
- return {
- pages: [{
- header: {
- description: strings.PropertyPaneDescription
- },
- groups: [{
- groupName: strings.BasicGroupName,
- groupFields: [
- PropertyPaneTextField('description', {
- label: strings.DescriptionFieldLabel
- })
- ]
- }]
- }]
- };
- }
- }
Run gulp serve to package the solution.
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.
This will deploy the list to SharePoint and will show a success message in the UI as shown below.
Heading over to site contents, we can see the newly created list.
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.