question

MauricioPV-0943 avatar image
0 Votes"
MauricioPV-0943 asked MauricioPV-0943 commented

Sharepoint online - copy files programmatically

Hello,

I'm working wiht Sharepoint Online, trying to copy a file (and his metadata) from one site to another in the same site collection, I'm using MoveCopyUtil:

             using (SP.ClientContext lContext = ClientContextAuth(xsURLOrigen))
             {
                 SP.MoveCopyOptions lOptions = new SP.MoveCopyOptions();
                 lOptions.ResetAuthorAndCreatedOnCopy = false;
                 SP.MoveCopyUtil.CopyFile(lContext, xsURLOrigen, xsURLDestino,true, lOptions);
                 lContext.ExecuteQuery();
             }

This throw the next error: "Value does not fall within the expected range."

any help will be appreciated, thanks!
Mauricio.

office-sharepoint-onlinesharepoint-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.

1 Answer

MichaelHan-MSFT avatar image
0 Votes"
MichaelHan-MSFT answered MauricioPV-0943 commented

Hi @MauricioPV-0943,

Per my test, the MoveCopyUtil works for me. Below is my demo code:

         var userName = "michael@tenant.onmicrosoft.com";
         var password = "xxxx";
         var siteUrl = "https://tenant.sharepoint.com/sites/test";
         SecureString securePassword=new SecureString();
         foreach(char a in password)
         {
             securePassword.AppendChar(a);
         }
         var credential = new SharePointOnlineCredentials(userName, securePassword);
         var ctx = new ClientContext(siteUrl);
         ctx.Credentials = credential;
         var srcUrl="https://tenant.sharepoint.com/sites/test/Shared%20Documents/aa.txt";
         var desUrl="https://tenant.sharepoint.com/sites/test/sub1/Shared%20Documents/aa.txt";

         MoveCopyOptions lOptions = new MoveCopyOptions();
         lOptions.ResetAuthorAndCreatedOnCopy = false;
         MoveCopyUtil.CopyFile(ctx, srcUrl, desUrl,true, lOptions);
         ctx.ExecuteQuery();


If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.



· 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 Michael!!

yes! I was missing a piece of source url, thanks for your answer!

cheers,
Mauricio.

0 Votes 0 ·