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
TypeScript
import { sp } from "@pnp/sp";
// In web part init
sp.setup({ spfxContext: this.context });Read Items
TypeScript
const items = await sp.web.lists
.getByTitle("Tasks")
.items
.select("Id", "Title", "Status")
.filter("Status eq 'Active'")
.top(100)
.get();Create Item
TypeScript
await sp.web.lists.getByTitle("Tasks").items.add({
Title: "New Task",
Status: "Active"
});Update Item
TypeScript
await sp.web.lists.getByTitle("Tasks")
.items.getById(1)
.update({ Status: "Completed" });Delete Item
TypeScript
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.