How to Move files from one document library to another in Sharepoint 2013/2016 using CSOM?

Hello Sharepointers,

In this blog,we will talk about CSOM code which moves documents from one document library to another.

var ctx= new SP.ClientContext.get_current();
var web = ctx.get_web();
var folderSource= web.getFolderByServerRelativeUrl(sourceLib);
function() {
var files = folderSource.get_files();
var e = files.getEnumerator();
var dest = [];

while (e.moveNext()) {
var file = e.get_current();
var destLibUrl = destLib + “/” + file.get_name();
file.moveTo(destLibUrl, SP.MoveOperations.overwrite);
context.executeQueryAsync(function() { console.log(“All the Files have been moved successfully!”);}, function(sender, args) {console.log(“error: “) + args.get_message()});
function(sender, args){console.log(“Sorry, something error happened: ” + args.get_message());}

Happy Sharepointing 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *