Skip to content

Instantly share code, notes, and snippets.

@simkessy
Created April 23, 2017 00:44
Show Gist options
  • Save simkessy/1aaea535e8c73876553371f9f2ab3e8a to your computer and use it in GitHub Desktop.
Save simkessy/1aaea535e8c73876553371f9f2ab3e8a to your computer and use it in GitHub Desktop.
pnp-js-core: Using Batching to checkout, update and checkin selected list items
// Update files
function updateItems (isTrue) {
// Get selected files from list
var selectedItems = SP.ListOperation.Selection.getSelectedItems().map(x => x.id)
var myFieldValue = isTrue ? "true" : "false";
var batch = $pnp.sp.createBatch(); // Create batch object
var fileRefs = []; // Store selected files reference
var documentLibrary = $pnp.sp.web.lists.getByTitle("Documents"); // Get document library
// Get fileRef from selected items
selectedItems.map(function(item) {
documentLibrary
.items
.getById(item)
.select("FileRef")
.inBatch(batch)
.get()
.then(x => fileRefs.push(x.FileRef))
})
// Check out selected files
fileRefs.map(function(fileRef) {
$pnp.sp.web.getFileByServerRelativeUrl(fileRef)
.inBatch(batch)
.checkout()
.then(() => {
console.log("File checked out:", fileRef);
});
})
// Update file property
selectedItems.map(function(itemID) {
documentLibrary
.items
.getById(itemID)
.inBatch(batch)
.update({
myBooleanField: myFieldValue
})
})
// Check in file
fileRefs.map(function(fileRef) {
$pnp.sp.web.getFileByServerRelativeUrl(fileRef)
.inBatch(batch)
.checkin()
.then(() => {
console.log("File checked in!");
});
})
// Execute batch?
batch.execute().then(() => console.log("Files have been updated successfully"));
}
updateItems()
@mahpour
Copy link

mahpour commented Nov 2, 2018

The fileRef on line 22 comes empty since previous block s set to run in the batch. so we will not have a fileRef until final batch executes. How does this work for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment