Please specify what version of the library you are using: [1.1.2]
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);
The code above leads to runtime error (not response error):
This query is already part of a batch.
Use the code above
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.
Most helpful comment
You'll likely need to keep grabbing the file object:
OR you could subclass File and make your own special method that does what you want using the protected clone method:
I didn't test this out, but should work for you.