Pnpjs: Unable to perform multiple operations with the same file in one batch.

Created on 13 Jul 2018  路  3Comments  路  Source: pnp/pnpjs

Category

  • [ ] Enhancement
  • [x] Bug
  • [ ] Question
  • [ ] Documentation gap/issue

Version

Please specify what version of the library you are using: [1.1.2]

Expected / Desired Behavior / Question

Code below works without errors:

const batch = sp.createBatch();
const file = sp.web.getFileByServerRelativeUrl(fileUrl);
file.inBatch(batch).checkout();
file.inBatch(batch).copyTo(desination, true);
file.inBatch(batch).undoCheckout();
batch.execute().then(doneHandler);

Observed Behavior

The code above leads to runtime error (not response error):

This query is already part of a batch.

Steps to Reproduce

Use the code above

code answered question

Most helpful comment

You'll likely need to keep grabbing the file object:

const web = sp.web;
const batch = web.createBatch();
web.getFileByServerRelativeUrl("").inBatch(batch).checkout();
web.getFileByServerRelativeUrl("").inBatch(batch).copyTo("", true);
web.getFileByServerRelativeUrl("").inBatch(batch).undoCheckout();
batch.execute().then(() => console.log("done"));

OR you could subclass File and make your own special method that does what you want using the protected clone method:

import { sp, File } from "@pnp/sp";

class MyFile extends File {
    public doBatchMove(batch: any, sourceFile: string, destination: string): void {
        this.clone(MyFile, null, false).inBatch(batch).checkout();
        this.clone(MyFile, null, false).inBatch(batch).copyTo("", true);
        this.clone(MyFile, null, false).inBatch(batch).undoCheckout();
    }
}

const file = sp.web.getFileByServerRelativeUrl("").as(MyFile);
const batch = sp.web.createBatch();
file.doBatchMove(batch, "", "");
batch.execute();

I didn't test this out, but should work for you.

All 3 comments

You'll likely need to keep grabbing the file object:

const web = sp.web;
const batch = web.createBatch();
web.getFileByServerRelativeUrl("").inBatch(batch).checkout();
web.getFileByServerRelativeUrl("").inBatch(batch).copyTo("", true);
web.getFileByServerRelativeUrl("").inBatch(batch).undoCheckout();
batch.execute().then(() => console.log("done"));

OR you could subclass File and make your own special method that does what you want using the protected clone method:

import { sp, File } from "@pnp/sp";

class MyFile extends File {
    public doBatchMove(batch: any, sourceFile: string, destination: string): void {
        this.clone(MyFile, null, false).inBatch(batch).checkout();
        this.clone(MyFile, null, false).inBatch(batch).copyTo("", true);
        this.clone(MyFile, null, false).inBatch(batch).undoCheckout();
    }
}

const file = sp.web.getFileByServerRelativeUrl("").as(MyFile);
const batch = sp.web.createBatch();
file.doBatchMove(batch, "", "");
batch.execute();

I didn't test this out, but should work for you.

@patrick-rodgers thanks for the answer!
I suspected that this approach will work :) Just was wondering if there is another way as in CSOM when you do bunch of operations on a single object and then execute a query.

Not in the way that we currently construct the batch.

Was this page helpful?
0 / 5 - 0 ratings