0

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);
 ctx.Load(item);
 ctx.ExecuteQuery();

 if (item.FileSystemObjectType == FileSystemObjectType.File)
 {
     var file = item.File;
     ctx.Load(file);
     ctx.ExecuteQuery();
     var r = new ResourcePath();

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

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.

1
  • Can you try to remove trailing slash in file.MoveTo: file.MoveTo("sites/afd-286-oprydning-test-target/TestDoc/Test01.docx", Movperations.Overwrite); – Slaven Semper Oct 6 '19 at 6:35
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;
    srcContext.Load(srcWeb);            
    srcContext.ExecuteQuery();

    Web destWeb = destContext.Web;
    destContext.Load(destWeb);
    destContext.ExecuteQuery();

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

        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);
6
  • 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. – Thomas Segato Oct 6 '19 at 7:53
  • You are welcome. Please mark this as correct answer and give vote. – Md. Nizam Uddin Mahmud Oct 6 '19 at 9:54
  • I just tested the code. Unfortuneantly it does not work with modern authentication and app registrations:( I get an 401. – Thomas Segato Oct 6 '19 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(); – Md. Nizam Uddin Mahmud Oct 6 '19 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. – Thomas Segato Oct 6 '19 at 18:56
0

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

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

1
  • If you add some sample code it will be helpful for the community, Thanks. – Md. Nizam Uddin Mahmud Oct 27 '19 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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