I have following code:

var sourceSite = "https://xx.sharepoint.com/sites/afd-286-oprydning-test-source";
var targetSIte = "https://xx.sharepoint.com/sites/afd-286-oprydning-test-target";

var ctx = ClientContextExtension.GetAppContext(sourceSite, _config.AppRegistrationId, _config.AppRegistrationKey, "SaveToExcel", new EmptyLogger());
Microsoft.SharePoint.Client.List list = ctx.Web.GetDocumentsList();
 var item = list.GetItemById(1);

 if (item.FileSystemObjectType == FileSystemObjectType.File)
     var file = item.File;
     var r = new ResourcePath();

     file.MoveTo("/sites/afd-286-oprydning-test-target/TestDoc/Test01.docx", Movperations.Overwrite);

I get following error:

Microsoft.SharePoint.Client.ServerException: 'Server relative urls must start with SPWeb.ServerRelativeUrl'

Any pointers how to move a file? I also tried a fully qualified url but no luck.

According to documentation: https://docs.microsoft.com/en-us/previous-versions/office/sharepoint-csom/mt780894(v%3Doffice.15)

You should use MoveToByPath but I havent found a single hit on a sample on google.

  • Can you try to remove trailing slash in file.MoveTo: file.MoveTo("sites/afd-286-oprydning-test-target/TestDoc/Test01.docx", Movperations.Overwrite); Oct 6, 2019 at 6:35

2 Answers 2


You can not move file using file.MoveTo method from one site to another site. There is no built-in method in SharePoint CSOM for moving Files from one site into another.

But you can use below procedure to move file.

public static void FileMove(string srcUrl, string destUrl, string srcLibrary, string destLibrary, string srcFileName)
    ClientContext srcContext = new ClientContext(srcUrl);
    ClientContext destContext = new ClientContext(destUrl);            

    Web srcWeb = srcContext.Web;

    Web destWeb = destContext.Web;

        var fileRelativePath = (srcWeb.ServerRelativeUrl.EndsWith("/") ? srcWeb.ServerRelativeUrl : srcWeb.ServerRelativeUrl + "/") + "Lists/" + srcLibrary + "/" + srcFileName;
        Microsoft.SharePoint.Client.File file = srcWeb.GetFileByServerRelativeUrl(fileRelativePath);

        string location = destWeb.ServerRelativeUrl.TrimEnd('/') + "/Lists/" + destLibrary + "/" + file.Name;
        FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(srcContext, file.ServerRelativeUrl);
        Microsoft.SharePoint.Client.File.SaveBinaryDirect(destContext, location, fileInfo.Stream, true);

    catch (Exception ex)
        throw ex;

Call this method with following parameters

var srcUrl = "https://xx.sharepoint.com/sites/SourceSite/";
var destUrl = "https://xx.sharepoint.com/sites/TargetSite/";
var srcLibrary = "Documents";
var destLibrary = "Documents";
var srcFileName = "Test.docx";

FileMove(srcUrl, destUrl, srcLibrary, destLibrary, srcFileName);
  • Thanks, that was what I feared:( Would have been so nice to only have a single operation due to throttles, where a pointer is just moved on the backend. I will use your approach, thanks alot. Oct 6, 2019 at 7:53
  • You are welcome. Please mark this as correct answer and give vote. Oct 6, 2019 at 9:54
  • I just tested the code. Unfortuneantly it does not work with modern authentication and app registrations:( I get an 401. Oct 6, 2019 at 16:03
  • May be you want to add authentication for the context. See this code for authentication: string userName = "YourUserName"; string password = "YourPssword"; SecureString securePassword = new SecureString(); foreach (char c in password.ToCharArray()) { securePassword.AppendChar(c); } srcContext.AuthenticationMode = ClientAuthenticationMode.Default; srcContext.Credentials = new SharePointOnlineCredentials(userName, securePassword); srcContext.ExecuteQuery(); Oct 6, 2019 at 17:02
  • I am using an app registration. It is a big enterprise customer. However I found another approach. The problem was the customer is using modern authentication. SaveBinaryDirect does not work with modern authentication. Oct 6, 2019 at 18:56

For others. It is actually possible with multiple different approaches. There is a CopyUtil in CSOM but also this one. Tested and works.


  • If you add some sample code it will be helpful for the community, Thanks. Oct 27, 2019 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.