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,655 questions
0 comments No comments
{count} votes

7 answers

Sort by: Most helpful
  1. Jerryzy 10,561 Reputation points
    2021-04-19T07:03:20.637+00:00

    Hi @Damien Barafort ,

    I can reproduce this issue in SharePoint 2019 site, SP.MoveCopyUtil.copyFile isn't working for copy file accross the site collection.

    A workaround would be using the Cross Domain Library "SP.RequestExecutor.js" for cross site collection issue, here is the capture about the code snippet, the red line box is the parameter need to edit with yours:

    88985-snipaste-2021-04-19-14-48-02.png

    For the full code snippet, please check the attachment below:

    88959-copy.txt

    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

  2. Damien Barafort 1 Reputation point
    2021-04-20T14:05:20.407+00:00

    Hi.
    Thank you very much for your answer; unfortunately until now it does not run . I got problems with:

    • urls provided to the functions (including hostname, relative, absolute, qith decoded url or not)
    • GetFileByServerRelativeUrl replaced in my code by GetFileByServerRelativePath (the first one did not work)
    • var result = data.body; seems useless (but that does not matter I think; notethat data.body is null)
    • the last request with : var restUrl=targetSiteUrl+"/_api/web/GetFolderByServerRelativeUrl('"+ folderName +"')/Files/Add(url='"+ newFileName +"',overwrite=true)"; does not run, go into error branch

    Have you got any idea? Code is so much more complicated, SPMoveCopyUtil was so easy... :-)

    0 comments No comments

  3. Jerryzy 10,561 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

  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. 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...)