copy or move replace file not working on CSOM Sharepoint

fredy setiawan 1 Reputation point
2020-09-08T09:18:18.783+00:00

hi im using method for copy or move file on sharepoint CSOM. but i want to ask how to implement when it's duplicated it automatically replace and update version?
i have been try with this function but it's always

"Message": "An error has occurred.",

"ExceptionMessage": "The destination file already exists.",
"ExceptionType": "Microsoft.SharePoint.Client.ServerException",
"StackTrace": "   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)\r\n   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()\r\n   at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)\r\n   at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()\r\n   at SRSHostedApp.Services.SharePointOnPremises.MoveFile(String SrcUrl, String DestUrl) in C:\\smart_registry_system\\sharepoint-integration\\SRSHostedApp\\SRSHostedApp\\Services\\SharePointOnPremises.cs:line 671\r\n   at SRSHostedApp.Controllers.FoldersController.MoveItem(MoveModel moves) in C:\\smart_registry_system\\sharepoint-integration\\SRSHostedApp\\SRSHostedApp\\Controllers\\FoldersController.cs:line 167\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_2.<GetExecutor>b__2(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()"

this is my function

public void CopyFile(string SrcUrl, string DestUrl)
    {
        MoveCopyOptions option = new MoveCopyOptions();
        option.KeepBoth = false;
        MoveCopyUtil.CopyFile(this.clientContext,SrcUrl,DestUrl,true,option);
        this.clientContext.ExecuteQuery();
    }

and my src url

http://*********:55555/sites/srsrms/SRS%20Documents/Finance/fredytest/License%20Management.csv

and dst url

http://********:55555/sites/srsrms/SRS%20Documents/Finance/paidi/Finance%20Folder/License%20Management.csv
SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,227 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,684 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,575 questions
SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,811 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Baker Kong-MSFT 3,791 Reputation points
    2020-09-09T09:21:39.743+00:00

    Hi @fredy setiawan ,

    I tested this method on my SP 2016 environment, it works well if I set 'overwrite' true. When both 'KeepBoth' and 'overwrite' are false, it will prompt 'The destination file already exists' :

    23408-0.png

    Below is my test demo:

    Best Regards,
    Baker Kong


    If an Answer 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.

    1 person found this answer helpful.

  2. Sharath Kumar Aluri 3,071 Reputation points
    2020-09-08T22:46:36.547+00:00

    Try with the below commands:

    Install-Module SharePointPnPPowerShell2016
    
    Copy-PnPFile -SourceUrl "http://*********:55555/sites/srsrms/SRS%20Documents/Finance/fredytest/License%20Management.csv" -TargetUrl "http://********:55555/sites/srsrms/SRS%20Documents/Finance/paidi/Finance%20Folder/License%20Management.csv" -OverwriteIfAlreadyExists -Force -Verbose
    

    Thanks & Regards,