0

I can't get the overwrite paramreter in SP.MoveCopyUtil.copyFile to work. I can't find any good documentation on it either. Heres the code.

appObj.copyFile = function (source, path, fileName) {
var fileParts = path.split(/\//);
fileParts.shift();
var destination = fileParts.join('/'), sourceParts = source.split(/\//);
sourceParts.shift();
var src = sourceParts.join('/'), 
    time = new Date(new Date().getTime() + 0 * 60 * 60 * 1000).toLocaleTimeString(),
    context = new SP.ClientContext(appObj.appWebUrl),
factory = new SP.ProxyWebRequestExecutorFactory(appObj.appWebUrl);
context.set_webRequestExecutorFactory(factory);
var appContextSite = new SP.AppContextSite(context, appObj.hostweburl);
this.web = appContextSite.get_web();
context.load(this.web);
console.log(appObj.host + source + '/' + fileName, appObj.host + destination + '/' + fileName);
SP.MoveCopyUtil.copyFile(context, (appObj.host + source + '/' + fileName), (appObj.host + '/' + destination + '/' + fileName), true);
context.executeQueryAsync(function (a, b) {
    var html;
    html = String.format("<p>{0}: <br> File: {1} copied from {2} <br> to  {3}</p>", time, fileName, source + '/' + fileName, destination + '/' + fileName);
    console.log(html);
    $("#logBox").append(html);
},
function (a, b) {
    var html,
        message = b.get_message();
    console.log(a);
    console.log(b);
    html = String.format("<p>{0}:</p><p>Error: {1}</p>", time, message);
    console.log(html);
    $("#logBox").append(html);
});
}

Any ideas of what I'm doing wrong? Typing SP.MoveCopyUtil.copyFile in the console gives me some information:

    function SP_MoveCopyUtil$copyFile(context, srcUrl, destUrl, overwrite)
  • Could you please paste full URLs of the source and destination variables – Roman Jun 14 '16 at 13:58
  • It works with copying files, so the URLs are correct. It's the overwrite parameter that doesn't work. But sure, I can post them if the formating of the URL has an impact on the overwrite parameter. – Chris Jun 15 '16 at 6:48
  • Looks like the source and destination URLs are the same – Roman Jun 15 '16 at 9:36
  • exactly. I get file already exsists error, but I want to overwrite – Chris Jun 15 '16 at 10:04
0

Please see an example below:

(function(from, to){
    var context = SP.ClientContext.get_current();
    SP.MoveCopyUtil.copyFile(context, from, to, true);
    context.executeQueryAsync(
        function(){ console.log("The file was copied successfully"); },
        function(s,a){console.log(a.get_message());}
    );
})("https://test.sharepoint.com/Shared%20Documents/Test%20File%20A4.pdf", "https://test.sharepoint.com/Shared%20Documents/Test%20File%20A4_2.pdf");
| improve this answer | |
  • Thank you, but this seems like exactly what I've done. Let me update my post to include all of the code, maybe you can spot what I'm missing. – Chris Jun 14 '16 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.