Forum Discussion

Shubham Mathur's avatar
Shubham Mathur
Copper Contributor
Sep 17, 2018

Copy a file from one Library to another library across Site collection using CSOM code

Hi

i have written this below code but getting a run time error. Please help me out. and i have include a button on click Button1_Click1 function will execute.

using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CopySCTaskWeb
{
    public partial class Default : System.Web.UI.Page
    {
        
        protected void Button1_Click1(object sender, EventArgs e)
        {
           
             string filePath = "/Document1/Doc1.docx";
             var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

             using (var sourceContext = spContext.CreateUserClientContextForSPHost())
             {
                 Microsoft.SharePoint.Client.File sourceFile = sourceContext.Web.GetFileByServerRelativeUrl(filePath);
                 //file stream will be used to upload in destination library
                 var fileStream = sourceFile.OpenBinaryStream();
                 sourceContext.Load(sourceFile, k => k.ServerRelativeUrl, k => k.Name);
                 sourceContext.ExecuteQuery();

                 //generate destination context
                 string destinationSiteUrl = "https://...";     //I used mysite url here
                 Uri destinationSiteUri = new Uri(destinationSiteUrl);

                 //target realm of the tenant (This is constant per tenant)
                 string targetRealm = TokenHelper.GetRealmFromTargetUrl(destinationSiteUri);

                 //generate access token for destination site
                 string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, destinationSiteUri.Authority, targetRealm).AccessToken;
                 //get destination site context using access token
                 using (var destinationContext = TokenHelper.GetClientContextWithAccessToken(destinationSiteUrl, accessToken))
                 {
                     //load server relative url of destination library
                     var destinationLibrary = destinationContext.Web.Lists.GetByTitle("Drop Off Library");

                     FileCreationInformation fileCreationInfo = new FileCreationInformation();
                     fileCreationInfo.ContentStream = fileStream.Value;
                     fileCreationInfo.Url = sourceFile.Name;
                     fileCreationInfo.Overwrite = false;
                     Microsoft.SharePoint.Client.File destinationFile = destinationLibrary.RootFolder.Files.Add(fileCreationInfo);
                     destinationContext.Load(destinationFile, k => k.ServerRelativeUrl);
                     destinationContext.ExecuteQuery();
                 }
             }
        }
    }
}

and i am getting an error i.e

1 Reply

  • Shubham Mathur's avatar
    Shubham Mathur
    Copper Contributor

    Hi ,

    above problem resolved. But , i am facing new problem i.e. the document copied to another site collection as a checkOut. How to do it without using ForceCheckedOut property .

    If it is possible , please share the code using csom.

     

    Thanks,

    Shubham