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);
ctx.load(folderSource,’Files’);
ctx.executeQueryAsync(
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();
dest.push(destLibUrl);
file.moveTo(destLibUrl, SP.MoveOperations.overwrite);
}
c
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.