Created
April 23, 2017 00:44
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
fileRef
on line 22 comes empty since previous block s set to run in the batch. so we will not have afileRef
until final batch executes. How does this work for you?