SharePoint Online Lists: CRUD Operations with SPFx

On this page

Interacting with SharePoint lists is the most common SPFx task. Here’s how to do it properly with PnP JS.

Setup PnP JS

TypeScript4 lines
import { sp } from "@pnp/sp";

// In web part init
sp.setup({ spfxContext: this.context });

Read Items

TypeScript7 lines
const items = await sp.web.lists
  .getByTitle("Tasks")
  .items
  .select("Id", "Title", "Status")
  .filter("Status eq 'Active'")
  .top(100)
  .get();

Create Item

TypeScript4 lines
await sp.web.lists.getByTitle("Tasks").items.add({
  Title: "New Task",
  Status: "Active"
});

Update Item

TypeScript3 lines
await sp.web.lists.getByTitle("Tasks")
  .items.getById(1)
  .update({ Status: "Completed" });

Delete Item

TypeScript3 lines
await sp.web.lists.getByTitle("Tasks")
  .items.getById(1)
  .delete();

References


Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Written by

Nithin Mohan TK

Hello Namaste! 👋🏻 I'm an accomplished Enterprise Solution Architect & Engineering Leader with 20+ years of progressive experience architecting, building, and scaling transformative enterprise solutions. ⚜️ Solution Architect / AI & Cloud 📌 Galway, Ireland

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.