question

DamienBarafort-8210 avatar image
0 Votes"
DamienBarafort-8210 asked DamienBarafort-8210 answered

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

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-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Jerryzy avatar image
0 Votes"
Jerryzy answered Jerryzy edited

Hi @DamienBarafort-8210 ,

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.




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DamienBarafort-8210 avatar image
0 Votes"
DamienBarafort-8210 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Jerryzy avatar image
0 Votes"
Jerryzy answered

Hi @DamienBarafort-8210 ,

  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.






5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DamienBarafort-8210 avatar image
0 Votes"
DamienBarafort-8210 answered Jerryzy commented

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @DamienBarafort-8210 ,
Can you do a test for "_api/web/GetFolderByServerRelativeUrl('/sites/.../"+ folderName +"')/Files" in browser directly, check to see if there is file data returned ?

If this endpoint is invalid then the adding file won't work as it's not finding the valid folder path.

Thanks
Best Regards

0 Votes 0 ·
DamienBarafort-8210 avatar image
0 Votes"
DamienBarafort-8210 answered Jerryzy commented

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

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @DamienBarafort-8210 ,
Add a console.log(ex) in error handler and check F12 Console to see the detailed error response, post the capture with us, if it's showing "Not Found" like this:

90141-snipaste-2021-04-22-09-08-06.png

This should be the folder relative url is invalid.

Thanks
Best Regards


0 Votes 0 ·

Hi @DamienBarafort-8210 ,

Is there any update ? Did you fix the problem for uploading file ?


Thanks
Best Regards

0 Votes 0 ·
DamienBarafort-8210 avatar image
0 Votes"
DamienBarafort-8210 answered

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



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DamienBarafort-8210 avatar image
0 Votes"
DamienBarafort-8210 answered

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.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.