Change source url and destination url before executing. On click on move button initListName() method can be called. Once SP.RequestExecutor.js is loaded Copy function can be done. This code can be used to copy files across site collection.
If copy is within site collection Function CopyTo can be directly used.
var hostweburl="https://xxx.sharepoint.com";//edit
function initListName() {
var scriptbase = hostweburl + "/_layouts/15/";
$.getScript(scriptbase + "/SP.RequestExecutor.js", copyFile);
}
function copyFile() {
var sourceUrl=sourceUrlParam;//provide source site url
var destUrl=destinatonUrlParam;//provide destination site url
// Create a request executor.
var sourceExecutor = new SP.RequestExecutor(sourceUrl);
var targetExecutor = new SP.RequestExecutor(destUrl);
var fileName="/sites/TestDemo/ProjectAttachments/Test%20123.docx";
//provide file name with path
var fileContentUrl=sourceUrl+"/_api/web/GetFileByServerRelativeUrl('"+fileName+"')/$value";
var targetSiteUrl=destUrl;
//var folderName='ProjectAttachments';
var newFileName="Test123.docx";//New name of added file
var folderName="/sites/TestDemo/ProjectAttachments";
//provide folder path to which file to be copied
var restUrl=targetSiteUrl+"/_api/web/GetFolderByServerRelativeUrl('"+folderName+"')/Files/Add(url='"+newFileName+"',overwrite=true)";
$.ajax({
url: targetSiteUrl + "/_api/contextinfo",
type: "POST",
headers: {
"Accept": "application/json;odata=verbose"
},
success: function (data) {
var digest = data.d.GetContextWebInformation.FormDigestValue;
// Build executor action to retrieve the file data.
var getFileAction = {
url: fileContentUrl,
method: "GET",
binaryStringResponseBody: true,
success: function (getFileData) {
// Get the binary data.
var result = data.body;
// Build executor action to copy the file data to the new location.
var copyFileAction = {
url: restUrl,
method: "POST",
headers: {
"Accept": "application/json; odata=verbose",
"X-RequestDigest": digest
},
contentType: "application/json;odata=verbose",
binaryStringRequestBody: true,
body: getFileData.body,
success: function(copyFileData) {
alert("successfully Copied");
},
error: function(ex) {
alert("Something went wrong, please try again later");
//show your 'failed' message
}
};
targetExecutor.executeAsync(copyFileAction);
},
error: function(ex) {
//fail
alert("Something went wrong, please try again later");
}
};
sourceExecutor.executeAsync(getFileAction);
},
error: function(ex) {
//fail
alert("Something went wrong, please try again later");
}
});
}