This post was most recently updated on July 29th, 2024
in this blog, we cover the PnP Bulk Update and Delete Items.
Brief information about SP-PnP-JS
PnPJS is an open-source solution with active community providing support for it. There is no SLA for the open-source tool support from Microsoft.
SP PnP JS are patterns and practices that the core JavaScript library offers. They are simplified common operations with SharePoint to help developers concentrate on business logic without worrying much about the underlying technical implementation. It contains fluent APIs to work with SharePoint REST APIs.
Getting Started
Install the library and required dependencies
1 |
npm install @pnp/sp --save |
This approach avoids multiple calls for the same list’s entity type name.
I have created id’s array and passing id’s to inBatch operation with PnP update method in loop, also we capturing the error in catch block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
//Import the library into your application and access the root sp object import { sp } from "@pnp/sp"; import "@pnp/sp/webs"; import "@pnp/sp/lists"; import "@pnp/sp/items"; let selectedIDs = [1,2,3,4,5...1000]; //Get all items of list let listName = sp.web.lists.getByTitle('List Name'); try { listName.getListItemEntityTypeFullName().then((entityTypeFullName: any) => { let createBatchRequest = sp.web.createBatch(); for (const ItemID of selectedIDs) { listName.items.getById(ItemID).inBatch(createBatchRequest) .update({ status: `Active` }, '*', entityTypeFullName); } createBatchRequest.execute().then((createResponse: any) => { console.log("All Item Updated") }).catch((error) => { this.setState({ hasError: true, messageError: "Please check app configuration settings for the list name and data type then try again." }); }); }); } catch (error) { this.setState({ hasError: true, messageError: "Please check app configuration settings for the list name and data type then try again."); } |
Limitations : We can not update more then 1000+ items at a time, it will throws this error message
“The current change set contains too many operations. A maximum number of ‘1000’ operations are allowed in a change set.”
I recommended to update items in chunks to make process fast and avoid error.
Delete items in bulk
There is two approach either we can delete items permanently or we can move to recycle bin.
I have created id’s array and passing id’s to inBatch operation with PnP recycle method in loop, also we capturing the error in catch block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//Import the library into your application and access the root sp object import { sp } from "@pnp/sp"; import "@pnp/sp/webs"; import "@pnp/sp/lists"; import "@pnp/sp/items"; let selectedIDs = [1, 2, 3, 4, 5...100]; //Get all items of list let listName = sp.web.lists.getByTitle('List Name'); try { listName.getListItemEntityTypeFullName().then((entityTypeFullName: any) => { let deleteBacthRequest = sp.web.createBatch(); for (const ItemID of selectedIDs) { listName.items.getById(ItemID).inBatch(deleteBacthRequest).recycle().then(r => { console.log("Moved on Recyle bin"); }); } deleteBacthRequest.execute() .then((deleteResponse: any) => { console.log("All items Moved on Recyle bin"); }).catch((error) => { this.setState({ hasError: true, messageError: "Please check app configuration settings for the list name and data type then try again." }); }); }); } catch (error) { this.setState({ hasError: true, messageError: "Please check app configuration settings for the list name and data type then try again." }); } |
Getting and Deleting a collection using filter
In this method we are getting deleting id’s by passing id to filter method. This method helps to delete items which are lookup in another list. if one list has reference on another list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
//Import the library into your application and access the root sp object import { sp } from "@pnp/sp"; import "@pnp/sp/webs"; import "@pnp/sp/lists"; import "@pnp/sp/items"; //DELETE BULK ITEMS, GETTING ALL ITEMS BY ID, IT WILL SEND TO RECYLE let deleteItemId = 8707; let listName = sp.web.lists.getByTitle("List Name"); let deleteBacthRequest = sp.web.createBatch(); try { listName.getListItemEntityTypeFullName().then((entityTypeFullName: any) => { listName.items.filter(`ContentItemId eq ${deleteItemId}`).get().then((allItems) => { //USE https://pnp.github.io/pnpjs/sp/items/#recycle to send recyle bin //USE https://pnp.github.io/pnpjs/sp/items/#delete to delete allItems.forEach(i => { listName.items.getById(i["ID"]).inBatch(deleteBacthRequest).recycle().then(r => { console.log("Moved on Recyle bin"); }); }); deleteBacthRequest.execute() .then((deleteResponse: any) => { console.log("All items Moved on Recyle bin"); }).catch((error) => { this.setState({ hasError: true, messageError: "Please check app configuration settings for the list name and data type then try again." }); }); }); }); } catch (error) { this.setState({ hasError: true, messageError: "Please check app configuration settings for the list name and data type then try again." }); } |
Limitations : We can not update more then 1000+ items at a time, it will throws this error message
“The current change set contains too many operations. A maximum number of ‘1000’ operations are allowed in a change set.”
I recommended to update items in chunks to make process fast and avoid error.
Hope you find it helpful. If you liked this article, then please share and comment.
Resources: