SharePoint Online: CRUD operations using SPFx and PnP JS

SharePoint Online: CRUD operations using SPFx and PnP JS

No comments

Loading

In this crud operations using SPFx tutorial, we will learn about how to implement CRUD operation in SharePoint Online using the SPFx framework and PnP JS library.

Key-Highlights: CRUD operations using SPFx

  • Create project folder process
  • Project scaffolding process
  • Install the PnP JS library process
  • Launch visual studio code editor
  • Event listener code for Create, Read, Update and Delete operation
  • CRUD code explanation in PnP JS andย  SPFx framework
  • Testing the CRUD operation web part from SharePoint Online page

The CRUD operations using SPFx and PnP JS step-by-step process

Below is the step-by-step process for CRUD operations using SPFx and PnP JS:

Step1: Create Folder: CRUD operations using SPFx and PnP JS

Create a new project directory using the below command:

C:\Temp\SPFx\React JS>md CRUD_PnPJS 

Go to the created folder directory by the below command:

C:\Temp\SPFx\React JS>cd CRUD_PnPJS

Step 2: Project scaffolding process โ€“ CRUD operations using SPFx and PnP JS

C:\Temp\SPFx\React JS\CRUD_PnPJS>yo @microsoft/sharepoint

Let's create a new SharePoint solution.
? What is your solution name? crud-pn-pjs
? Which baseline packages do you want to target for your component(s)? SharePoint Online only (latest)
? Where do you want to place the files? Use the current folder
Found npm version 6.14.7
? Do you want to allow the tenant admin the choice of being able to deploy the solution to all sites immediately without
running any feature deployment or adding apps in sites? No
? Will the components in the solution require permissions to access web APIs that are unique and not shared with other c
omponents in the tenant? No
? Which type of client-side component to create? WebPart
Add new Web part to solution crud-pn-pjs.
? What is your Web part name? SP Online CRUD Using PnP JS
? What is your Web part description? SP Online CRUD Using PnP JS description
? Which framework would you like to use? (Use arrow keys)
> No JavaScript framework
React
Knockout
CRUD operations using SPFx and PnP JS, SPFX CRUD PnP JS - Congratulations your solution is created message
SPFX CRUD PnP JS โ€“ Congratulations your solution is created message

Now, need to run the below command:

Step 3: Install the PnP JS library โ€“ CRUD operations using SPFx and PnP JS

Run the below command to install the PnP JS library.

npm install sp-pnp-js โ€“save

Then run the Gulp Build command to check the solution was scaffolded properly without any error.

CRUD operations using SPFx and PnP JS, SPFx Gulp build command to check the project scaffolding error
SPFx Gulp build command to check the project scaffolding error

Create Employee List In SharePoint Online site with the below columns:

  1. EmpName โ€“ Single Line of Text
  2. EmpDepartment โ€“ Single Line of Text
CRUD operations using SPFx and PnP JS
Create Employee List In SharePoint Online site

Open the project solution code in the visual studio editor by typing โ€œcode .โ€ in the node js command prompt like below:

Step 4: Open code in visual studio editor by running the โ€œcode . โ€ command (CRUD operations using SPFx and PnP JS)

C:\Temp\SPFx\React JS\CRUD_PnPJS>code .

Now, letโ€™s run the project by running the gulp serve command.

We can see that the SP Online CRUD operation using PnP JS is displayed on the SharePoint page โ€“ with the Add, Update and Delete button.
SharePoint Online CRUD operation UI using PnP JS

CRUD operations using SPFx and PnP JS in SharePoint Online
CRUD operations using SPFx and PnP JS in SharePoint Online

Here, I will explain the code โ€“ how it was developed.

Event listener code for CREATE, READ, UPDATE and DELETE operation (CRUD operations using SPFx and PnP JS in SharePoint Online)

privateย AddEventListeners()ย :ย void

{ย ย ย ย 

ย ย ย 

ย ย document.getElementById('AddItemToSPList').addEventListener('click',()=>this.AddSPListItem());ย ย ย ย 

ย ย document.getElementById('UpdateItemInSPList').addEventListener('click',()=>this.UpdateSPListItem());ย ย ย ย 

ย ย document.getElementById('DeleteItemFromSPList').addEventListener('click',()=>this.DeleteSPListItem());ย ย ย ย 

ย }ย ย ย ย 

ย ย ย ย ย 

ย ย privateย _getSPItems():ย Promise<ISPList[]>ย {ย ย ย ย 

ย ย returnย pnp.sp.web.lists.getByTitle("Employee").items.get().then((response)ย =>ย {ย ย ย ย 

ย ย ย ย ย ย ย ย 

ย ย ย ย ย returnย response;ย ย ย ย 

ย ย });ย ย ย ย 
ย ย ย ย ย ย ย ย ย ย 

ย }ย ย ย ย 

ย ย ย ย ย 

ย ย privateย getSPItems():ย voidย {ย ย ย ย 

ย ย ย ย ย ย ย ย 

ย ย ย ย ย this._getSPItems()ย ย ย ย 

ย ย ย ย ย ย ย .then((response)ย =>ย {ย ย ย ย 

ย ย ย ย ย ย ย ย ย this._renderList(response);ย ย ย ย 

ย ย ย ย ย ย ย });ย ย ย ย 

ย }ย 

AddSPListItem()ย ย ย ย 

ย ย ย {ย ย ย ย ย ย 

ย ย ย ย ย ย ย 

ย ย ย ย ย ย ย ย pnp.sp.web.lists.getByTitle('Employee').items.add({ย ย ย ย ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย EmpNameย :ย document.getElementById('EmpName')["value"],ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย EmpDepartmentย :ย document.getElementById('EmpDepartment')["value"]ย ย 

ย ย ย ย ย ย ย ย ย ย ย 

ย ย ย ย ย ย ย });ย ย ย 

ย ย ย ย ย 

ย ย ย ย ย ย ย ย alert("Recordย withย Employeeย Nameย :ย "+ย document.getElementById('Employee')["value"]ย +ย "ย Addedย !");ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย 

ย ย ย }ย ย ย ย 

UpdateSPListItem()ย ย ย ย 

{ย ย ย ย ย ย 

ย varย empIDย =ย ย this.domElement.querySelector('input[nameย =ย "empID"]:checked')["value"];ย ย 

ย ย ย ย pnp.sp.web.lists.getByTitle("Employee").items.getById(empID).update({ย ย ย ย 

ย ย ย ย ย EmpNameย :ย document.getElementById('EmpName')["value"],ย ย ย ย 

ย ย ย ย ย EmpDepartmentย :ย document.getElementById('EmpDepartment')["value"]ย ย 

ย ย ย ย ย ย ย 

ย ย });ย ย ย ย 

ย alert("Recordย withย Employeeย IDย :ย "+ย empIDย +ย "ย Updatedย !");ย ย ย ย 

}ย ย ย ย 

ย ย ย ย 

DeleteSPListItem()ย ย ย ย 

{ย ย ย ย ย ย 

ย varย empIDย =ย ย this.domElement.querySelector('input[nameย =ย "empID"]:checked')["value"];ย ย 

ย ย ย 

ย ย ย ย ย pnp.sp.web.lists.getByTitle("Employee").items.getById(empID).delete();ย ย ย ย 

ย ย ย ย ย alert("Recordย withย Employeeย IDย :ย "+ย empIDย +ย "ย Deletedย !");ย ย ย ย 

}ย ย ย 

ย 

SpOnlineCrudUsingPnPJsWebPart.ts file โ€“ here is the completed code located likeย 
importย pnpย 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ย './SpOnlineCrudUsingPnPJsWebPart.module.scss';ย ย ย ย 

ย importย *ย asย stringsย fromย 'SpOnlineCrudUsingPnPJsWebPartStrings';ย ย ย ย 

ย ย ย ย 

export interface ISpOnlineCrudUsingPnPJsWebPartProps
{ย 

ย ย description:ย string;ย ย 

}ย ย 
ย 

exportย interfaceย ISPList
ย {ย ย ย ย 

ย ย ย ย ID:ย string;ย ย ย ย 

ย ย ย ย EmpName:ย string;ย ย ย ย 

ย ย ย ย EmpDepartment:ย string;ย ย ย ย 

ย ย }ย ย ย ย ย 
ย ย ย ย 

export default class SpOnlineCrudUsingPnPJsWebPart extends BaseClientSideWebPart<ISpOnlineCrudUsingPnPJsWebPartProps>

ย ย {ย ย ย ย 
ย ย 
ย ย ย 

private AddEventListeners() : void
{ย 

ย ย document.getElementById('AddItemToSPList').addEventListener('click',()=>this.AddSPListItem());ย ย ย ย 

ย ย document.getElementById('UpdateItemInSPList').addEventListener('click',()=>this.UpdateSPListItem());ย ย ย ย 

ย ย document.getElementById('DeleteItemFromSPList').addEventListener('click',()=>this.DeleteSPListItem());ย ย ย ย 

ย }ย ย ย ย 

ย ย ย ย ย 

privateย _getSPItems():ย Promise<ISPList[]>
ย {ย ย ย ย 

ย ย returnย pnp.sp.web.lists.getByTitle("Employee").items.get().then((response)ย =>ย {ย ย ย ย 

ย ย ย ย ย ย ย ย 

ย ย ย ย ย returnย response;ย ย ย ย 

ย ย ย });ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย 

ย }ย ย ย ย 
ย ย ย 

privateย getSPItems():ย void
ย {ย ย ย ย 
ย ย ย 

ย ย ย ย ย this._getSPItems()ย ย ย ย 

ย ย ย ย ย ย ย .then((response)ย =>ย {ย ย ย ย 

ย ย ย ย ย ย ย ย ย this._renderList(response);ย ย ย ย 

ย ย ย ย ย ย ย });ย ย ย ย 

}ย ย ย  ย ย ย ย ย 

ย privateย _renderList(items:ย ISPList[]):ย void
ย {ย ย ย ย 

ย ย ย letย html:ย stringย =ย '<tableย class="TFtable"ย border=1ย width=style="bordercollapse:ย collapse;">';ย ย ย ย 

ย ย ย htmlย +=ย `<th></th><th>ID</th><th>Name</th><th>Department</th>`;ย ย ย ย 

ย ย ย ifย (items.length>0)ย ย 

ย ย ย {ย ย 

ย ย ย items.forEach((item:ย ISPList)ย =>ย {ย ย ย ย 

ย ย ย ย ย htmlย +=ย `ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย <tr>ย ย ย 

ย ย ย ย ย ย ย ย ย ย <td>ย ย <inputย type="radio"ย id="empID"ย name="empID"ย value="${item.ID}">ย <br>ย </td>ย ย ย 

ย ย ย ย ย ย ย ย ย ย 

ย ย ย ย ย ย ย ย ย <td>${item.ID}</td>ย ย ย ย 

ย ย ย ย ย ย ย ย ย <td>${item.EmpName}</td>ย ย ย ย 

ย ย ย ย ย ย ย ย ย <td>${item.EmpDepartment}</td>ย ย ย ย 

ย ย ย ย ย ย ย ย ย </tr>ย ย ย ย 

ย ย ย ย ย ย ย ย ย `;ย ย ย ย ย 

ย ย ย });ย ย ย ย 

ย ย }ย ย 

ย ย elseย ย ย ย 

ย ย 

ย ย {ย ย 

ย ย ย ย htmlย +="Noย records...";ย ย 

ย ย }ย ย 

ย ย ย htmlย +=ย `</table>`;ย ย ย ย 

ย ย ย constย listContainer:ย Elementย =ย this.domElement.querySelector('#DivGetItems');ย ย ย ย 

ย ย ย listContainer.innerHTMLย =ย html;ย ย ย ย 

ย }ย ย ย ย ย ย ย ย ย 
ย ย ย 

public render(): void
{ย ย ย ย 

ย ย ย ย ย this.domElement.innerHTMLย =ย `ย ย ย ย 

ย ย ย ย ย <divย class="parentContainer"ย style="background-color:ย white">ย ย ย ย 

ย ย ย ย <divย class="ms-Grid-rowย ms-bgColor-themeDarkย ms-fontColor-whiteย ${styles.row}">ย ย ย ย 

ย ย ย ย ย ย ย <divย class="ms-Grid-colย ms-u-lgย ย ย 

ย ย ย ms-u-xl8ย ms-u-xlPush2ย ms-u-lgPush1">ย ย ย 

ย ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย 

ย ย ย ย ย ย ย </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:ย ย ย 

ย ย ย x;">Employeeย Details</div>ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย 

ย ย ย ย </div>ย ย ย ย 

ย ย ย ย <divย style="background-color:ย white"ย >ย ย ย ย 

ย ย ย ย ย ย ย <formย >ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย <br>ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย <divย data-role="header">ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย <h3>Addย itemย toย SharePointย List</h3>ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย </div>ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย <divย data-role="main"ย class="ui-content">ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย <divย >ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย <inputย id="EmpName"ย ย placeholder="EmpName"/>ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย <inputย id="EmpDepartment"ย ย placeholder="EmpDepartment"/>ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย <buttonย id="AddItemToSPList"ย ย type="submit"ย >Add</button>ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย <buttonย id="UpdateItemInSPList"ย type="submit"ย >Update</button>ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย <buttonย id="DeleteItemFromSPList"ย ย type="submit"ย >Delete</button>ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย </div>ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย </div>ย ย ย ย 

ย ย ย ย ย ย ย </form>ย ย ย ย 

ย ย ย ย </div>ย ย ย ย 

ย ย ย ย <br>ย ย ย ย 

ย ย ย ย <divย style="background-color:ย white"ย id="DivGetItems"ย />ย ย ย ย 

ย ย ย ย ย ย 

ย ย ย ย </div>ย ย ย 

ย ย ย ย `;ย ย ย ย 

ย ย ย 

ย this.getSPItems();ย ย ย ย 

ย this.AddEventListeners();ย ย ย ย 

ย ย ย }ย ย ย ย 

ย ย ย ย ย 

ย ย ย AddSPListItem()ย ย ย ย 

ย ย ย {ย ย ย ย ย ย 

ย ย ย ย ย ย ย 

ย ย ย ย ย ย ย ย pnp.sp.web.lists.getByTitle('Employee').items.add({ย ย ย ย ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย EmpNameย :ย document.getElementById('EmpName')["value"],ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย EmpDepartmentย :ย document.getElementById('EmpDepartment')["value"]ย ย 

ย ย ย ย ย ย ย ย ย ย ย 

ย ย ย ย ย ย ย });ย ย ย 

ย ย ย ย ย 

ย ย ย ย ย ย ย ย alert("Recordย withย Employeeย Nameย :ย "+ย document.getElementById('Employee')["value"]ย +ย "ย Addedย !");ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย 

ย ย ย }ย ย ย ย 

UpdateSPListItem()ย ย ย ย 

{ย ย ย ย ย ย 

ย varย empIDย =ย ย this.domElement.querySelector('input[nameย =ย "empID"]:checked')["value"];ย ย 

ย ย ย ย pnp.sp.web.lists.getByTitle("Employee").items.getById(empID).update({ย ย ย ย 

ย ย ย ย ย EmpNameย :ย document.getElementById('EmpName')["value"],ย ย ย ย 

ย ย ย ย ย EmpDepartmentย :ย document.getElementById('EmpDepartment')["value"]ย ย 

ย ย ย ย ย ย ย 

ย ย });ย ย ย ย 

ย alert("Recordย withย Employeeย IDย :ย "+ย empIDย +ย "ย Updatedย !");ย ย ย ย 

}ย ย ย ย 

ย ย ย ย 

DeleteSPListItem()ย ย ย ย 

{ย ย ย ย ย ย 

ย varย empIDย =ย ย this.domElement.querySelector('input[nameย =ย "empID"]:checked')["value"];ย ย 

ย ย ย 

ย ย ย ย ย pnp.sp.web.lists.getByTitle("Employee").items.getById(empID).delete();ย ย ย ย 

ย ย ย ย ย alert("Recordย withย Employeeย IDย :ย "+ย empIDย +ย "ย Deletedย !");ย ย ย ย 

}ย ย ย 


ย 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ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย })ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ]ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย }ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ]ย ย ย ย 

ย ย ย ย ย ย ย ย ย }ย ย ย ย 

ย ย ย ย ย ย ย ]ย ย ย ย 

ย ย ย ย ย };ย ย ย ย 

ย ย ย }ย ย ย ย 

ย }ย ย 


Code explanation: CRUD operations using SPFx and PnP JS in SharePoint Online

AddEventListeners() โ€“ List of button event listeners on the page, this will associate the buttonโ€™s events to their related methods โ€“ for example, Add, Update, Delete, etc.

getSPItems() โ€“ This will retrieve the list items from the SharePoint list (here it is โ€“ โ€œEmployeeโ€ list) and display it within the div element declared in the render method.

AddSPListItem() โ€“ This method is responsible for adding a new item to SharePoint list.

UpdateSPListItem() โ€“ This method is responsible for updating an existing item from the SharePoint list, here item ID is the key parameter.

DeleteSPListItem() โ€“ This method is responsible for deleting an existing item from the SharePoint list, here item ID is the key parameter.

Testing the web part from the SharePoint Online Page (CRUD operations using SPFx and PnP JS in SharePoint Online)

Addition Test:

Add list item to SharePoint Online employee list โ€“ enter employee name as โ€œAdiโ€ and Department as โ€œHRโ€, then click on the โ€œAddโ€ button.

CRUD operations using SPFx and PnP JS in SharePoint Online, Add list item to SharePoint Online employee list - test
Add list item to SharePoint Online employee list โ€“ test

Add list item to SharePoint Online employee list -CRUD operations using SPFx and PnP JS in SharePoint Onlineย โ€“ test

Add list item to SharePoint Online employee list โ€“ after addition (Read Items) โ€“ we can see that the new employee โ€œAdiโ€ is added to the list.

CRUD operations using SPFx and PnP JS in SharePoint Online, Add list item to SharePoint Online employee list - after addition (Read Items)
CRUD operations using SPFx and PnP JS in SharePoint Online
Add list item to SharePoint Online employee list โ€“ after addition (Read Items)

Deletion Test:

CRUD operations using SPFx and PnP JS in SharePoint Online, Delete item from SharePoint list using PnP JS - test
Delete item from SharePoint list using PnP JS โ€“ Test

Summary: CRUD operations using SPFx and PnP JS in SharePoint Online

Thus, in this article, we have learned about how we can perform the CRUD operation in SharePoint Online using the PnP JS library in the SharePoint Framework (SPFx).

Download Source Code: CRUD operations using SPFx and PnP JS in SharePoint Online

The above source code file can be downloaded from here.

See Also: SPFx Framework Tutorial

You may also like the below SharePoint SPFx articles:

Download SharePoint Online PDF Book

Download SharePoint Online & Office 365 Administration eBook

Buy the premium version of SharePoint Online & Office 365 administration eBook from here:



Buy SharePoint Online & Office 365 Administration eBook


ย 

Get the free demo PDF eBook from here:

FREE DOWNLOAD

Send download link to:

Subscribe to get exclusive content and recommendations every month. You can unsubscribe anytime.

About Post Author


Discover more from Global SharePoint

Subscribe to get the latest posts sent to your email.

Do you have a better solution or question on this topic? Please leave a comment