'Attempting to add multiple items to SP list using sp/pnp but getting error on ```createBatch()```
I'm following this documentation: https://pnp.github.io/pnpjs/sp/items/#add-multiple-items But I'm getting an error with:
import { SPFI, spfi, SPFx } from "@pnp/sp";
import "@pnp/sp/webs";
import "@pnp/sp/lists";
import "@pnp/sp/items";
import "@pnp/sp/comments"
import "@pnp/sp/site-users/web";
let sp: SPFI;
export const CreateTableItems = async (listName: string, items: IMoscow) => {
const batch = sp.web.createBatch()
};
It's saying Property 'createBatch' does not exist on type 'IWeb & IInvokable<any>'.
I'm clearly missing something but the docs don't make it clear. I'm using the latest v3 version of sp/pnp and I am able to submit/update single items fine.
Solution 1:[1]
Maybe you've lost this import. import "@pnp/sp/batching";
Solution 2:[2]
It seems createBatch is not part of sp.web and we need to use createBatch function and provide the list object as parameter. In my scenario, I wanted to delete multiple items using batching and implemented as below
import { SPFI, spfi, SPFx } from "@pnp/sp";
import "@pnp/sp/webs";
import "@pnp/sp/lists";
import "@pnp/sp/items";
import "@pnp/sp/batching";
import "@pnp/sp/items/get-all";
import { createBatch } from "@pnp/sp/batching";
let sp: SPFI;
export const DeleteItems = async (listName: string) => {
const list = await sp.web.lists.getByTitle(listName);
const items = await list.items.getAll();
const [batchedListBehavior, execute] = createBatch(list);
list.using(batchedListBehavior);
items.forEach((i: IItem) => {
list.items.getById(i["ID"]).delete();
});
await execute();
};
Ref- Advanced Batching
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | txalvita |
Solution 2 | Harminder Singh |