0

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.

NightTom
  • 418
  • 15
  • 37

2 Answers2

1

Maybe you've lost this import. import "@pnp/sp/batching";

https://learn.microsoft.com/es-es/sharepoint/dev/spfx/web-parts/guidance/use-sp-pnp-js-with-spfx-web-parts

txalvita
  • 26
  • 2
  • I have imported import "@pnp/sp/batching" but still getting the error - Property 'createBatch' does not exist on type 'IWeb & IInvokable' – Harminder Singh Apr 27 '22 at 13:24
0

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

Harminder Singh
  • 183
  • 1
  • 8
  • I've been told that createBatch is not working as intended when it comes to sp pnp on V3. There are few things which don't seem to be working either. I'm considering downgrading to V2 for my projects in future until these issues are resolved. – NightTom Apr 28 '22 at 08:02