pnp / pnpjs

Fluent JavaScript API for SharePoint and Microsoft Graph REST APIs
https://pnp.github.io/pnpjs/
Other
753 stars 304 forks source link

Batching multiple lists fail #3025

Closed jsilva74 closed 5 months ago

jsilva74 commented 5 months ago

What version of PnPjs library you are using

2.x (No longer supported)

Minor Version Number

2.15.0

Target environment

NodeJS

Additional environment details

I'm authenticating with pnp-auth package using credentials which uses @pnp/sp-commonjs (v2.15.0). Node version is 20.12.2.

Question/Request

Hello and appologies for my English - it's isn't my first (or second) language even.

I need to update 3 lists in one batch in a service which run daily and I'm getting the famous "This query is already part of a batch" error. Already had searched about it, read all mentions to it (docs, threads, etc) I've found. Now, I'm in the "begging for help" phase. So, my service has the follow structure (simplified for brevity):

async function handleAnomalies(sp: SharePoint, batch: SPBatch, actionsToUpdate: any[]) {
  const list = sp.web.inBatch(batch).lists.getByTitle('List1')
  // handle all logic and, after, inside a for loop, attempt to add the item to update:
  await list.items.getById(anomaly.Id).update({ Status })
}

async function handleActions(sp: SharePoint, batch: SPBatch) {
  const list = sp.web.inBatch(batch).lists.getByTitle('List2')
  const notification : any[] = [] // these are the items that will be formated and inserted into another list latter
  const actionsToUpdate: any[] = [] // these are the list items needed to update
  for (const { Id, Status } of actionsToUpdate) {
    await list.items.getById(Id).update({ Status })
  }
  return { actionsToUpdate, notification }
}

async function handleNotifications(sp: SharePoint, batch: SPBatch, notification: any[]) {
  const list = sp.web.inBatch(batch).lists.getByTitle('List3')
  // format each notification and attempt to add it to list items (always using the batch) in a for loop
  await list.items.add({ PeopleId: { results: PeopleId }, Title, Body, Reason }) // PeopleId is an array of ids
}

async function handle() {
  const sp = new SharePoint() // this is a class that handle authentication and exposes pnpjs functionalities
  const batch = await sp.web.createBatch()
  const { actionsToUpdate, notifications } = await this.handleActions(sp, batch)
  await this.handleAnomalies(sp, batch, actionsToUpdate)
  await this.handleNotifications(sp, batch, notification)
  await batch.execute()
}

The error occurrs if more than 1 sub-functions have any item to add or update, the next function fail with "This query is already part of a batch" (the first runs ok but the follow function fail).

If anyone could be kind enough to point me where/what I'm doing wrong and help me with this, I'll deeply appreciate. Good day/afternoom/evening to all.

PS: I'm aware that v2 isn't supported anymore but, anyway, ya all get my point...

bcameron1231 commented 5 months ago

Hi, have you reviewed this comment from this previous issue? I suspect the answer here applies to your situation as you are using the same queryable in each of your batches (sp.web).

https://github.com/pnp/pnpjs/issues/1711#issuecomment-828546078

jsilva74 commented 5 months ago

Hi and thank you! I'll make the adjustments as proposed in the comment you've pointed out and see if it solves the problem.

Thanks again.

jsilva74 commented 5 months ago

@bcameron1231 hi.

Sorry the delay but I had to wait a couple days to have some data to run the test. Worked but, for future reference, chain .inBatch before the action as sugested, didn't work - I had to keep it before the lists method e.g.:

sp.web.inBatch(batch).lists.getByTitle('List1').add()
sp.web.inBatch(batch).lists.getByTitle('List1').update()

Again, thank you.

github-actions[bot] commented 4 months ago

This issue is locked for inactivity or age. If you have a related issue please open a new issue and reference this one. Closed issues are not tracked.