Quantcast
Channel: Microsoft Online: SharePoint Online forum
Viewing all articles
Browse latest Browse all 10096

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

$
0
0

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



Viewing all articles
Browse latest Browse all 10096

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>