SharePoint 2019 javascript SP.MoveCopyUtil.copyFile not working across site collections

Damien Barafort 1 Reputation point
2021-04-18T18:52:51.447+00:00

In SharePoint 2016, I was happily surprised to benefit new javascript functions available in SharePoint : SP.MoveCopyUtil.* to get out of the hell of copying files in SharePoint.

The following peace of code was working like a charm : var context = SP.ClientContext.get_current(); SP.MoveCopyUtil.copyFile(context,fileUrl,destUrl);

even when destUrl being in another site collection.

Unfortunately, after the migration to SharePoint 2019, it does not work anymore. It works only if destUrl is in the same site collection. How can I do? I there a problem with SharePoint 2019 js files?

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,673 questions
0 comments No comments
{count} votes

7 answers

Sort by: Newest
  1. Damien Barafort 1 Reputation point
    2021-10-20T10:22:57.377+00:00

    Hi!
    Unfortunately :

    • I forgot I had another automatic process, copying folders... So, same bug here; and replacing a simple line of code with SPMoveCopyUtil with a recursive call (recursive because of looping on files and javascript async way of doing) is in my opinion too much job for the money (next step : writing assembly language for the stuff...)
    • last SharePoint updates, applied to the farm, did not correct that bug

    So please Microsoft, ... DELIVER. Correct the bugs, including SharePoint on premise versions. Please.

    0 comments No comments

  2. Damien Barafort 1 Reputation point
    2021-09-21T09:57:54.36+00:00

    Hi. Thanks a lot for your support Jerryzy, sorry for the delay but ...

    So... After many many tests, here are my conclusions :

    • GetFileByServerRelativePath , GetFileByServerRelativeUrl, GetFolderByServerRelativeUrl ... do not work as soon as it is performed in another site collection (same as SPMoveCopyUtil). So I could not use your example, and then...
    • I tried to use SPServices, I failed
    • At last I used SP.FileCreationInformation using the same technique as in your example (loading the file in memory and then creating the file at the destination) and finally ... it run!!! ...
    • until a big file had to be copied, I had to change the server limits for API size limit (https://sharepoint.stackexchange.com/questions/111573/sharepoint-2013-javascript-code-for-file-upload-max-limit-exceeded/157307)

    I am happy it runs nows, but I am really disappointed when I think about all the time spent. Javascript + API bugs -> lot of pain :-)

    0 comments No comments

  3. Damien Barafort 1 Reputation point
    2021-04-21T15:17:53.603+00:00

    Hi !
    Yes, there are results. Only the copyFileAction fails. I asked myself if some other arguments were wrong (bad digest, bad enconding...)


  4. Damien Barafort 1 Reputation point
    2021-04-21T09:35:31.23+00:00

    Hi.
    Thank you very much for your answers! Happy for receiving your help.

    I did many tests, as I try to copy file into a subfolder of the target library; more over library and folder names contain special characters. My last test was to copy in the rootfolder of a library without special characters, no mure luck.

    Here is the code, if you detect somethingdo not hesitate to tell me:

    // var fileContentUrl=sourceUrl+"/_api/web/GetFileByServerRelativeUrl('"+fileRelativeUrl+"')/$value";
    var fileContentUrl=sourceUrl+"/_api/web/GetFileByServerRelativePath(DecodedUrl=@f)/$value?@f='"+fileRelativeUrl+"'";

    var targetSiteUrl=destUrl;
    
    
    var newFileName=fileName;//New name of added file
    var restUrl=targetSiteUrl+"_api/web/GetFolderByServerRelativeUrl('/sites/.../"+ 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;           
            var getFileAction = {
                url: fileContentUrl,
                method: "GET",
                binaryStringResponseBody: true,
                success: function (getFileData) {
    
                    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: copyEnded,
                        error: copyEnded
                    };
    
                    targetExecutor.executeAsync(copyFileAction);
                },
                error: copyEnded
            };
            sourceExecutor.executeAsync(getFileAction);
        },
        error: copyEnded
    });
    

    }

    function copyEnded(file,sender,args) {
    ...

    So GetFileByServerRelativeUrl did not run, the line with GetFileByServerRelativePath is ok.
    The first request is ok (digest is ok), getFileAction is ok, the copyFileAction fails, the error returned when adding the file is "NOT FOUND"

    Thanks in advance.

    Sincerely yours,

    Damien


  5. Jerryzy 10,566 Reputation points
    2021-04-21T01:06:22.923+00:00

    Hi @Damien Barafort ,

    1. Let me explain the relative url used in "GetFileByServerRelativeUrl", this should be the file relative url, combine with this format:

    "siterelativeurl"+"LibraryName"+"FileName"

    The site relative url you can get with _spPageContextInfo.siteServerRelativeUrl

    1. var result = data.body; is used for a debug propose to see the return data, you can ignore this line.
    2. The last request with GetFolderByServerRelativeUrl, make sure the folderName variable have been replaced with yours, then it will run as expected.

    If still having the issue, provide the siteurl and library folder name with us so that we can modify the necessary parameter for you.

    Thanks
    Best Regards


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments