Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batching multiple lists fail #3025

Closed
jsilva74 opened this issue May 2, 2024 · 4 comments
Closed

Batching multiple lists fail #3025

jsilva74 opened this issue May 2, 2024 · 4 comments

Comments

@jsilva74
Copy link

jsilva74 commented May 2, 2024

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
Copy link
Collaborator

bcameron1231 commented May 2, 2024

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).

#1711 (comment)

@jsilva74
Copy link
Author

jsilva74 commented May 2, 2024

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
Copy link
Author

jsilva74 commented May 6, 2024

@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.

@jsilva74 jsilva74 closed this as completed May 6, 2024
Copy link

github-actions bot commented Jun 1, 2024

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.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants