I'm trying to copy a SharePoint page in page library and copy it to another site, i tried use copyTo method but only works for files in the same site and i want to copy to anothe site in the same sitecollection

Here is my code:

using (ClientContext clientcontext = new ClientContext("https://server/siteSource")) {
  using (ClientContext clientcontext2 = new ClientContext("https://server/siteDestination")) { 
  //Activate sharepoint server publishing to create page library y siteDestination
    Guid guid = new Guid("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb");
    var web = clientcontext2.Web;
    var webSrc = clientcontext.Web;
    web.Features.Add(guid, true, FeatureDefinitionScope.None);
   //search page library in siteSource
   foreach (List list in clientcontext.Web.Lists) {
      if (list.Title.ToString() == "Pages") {
          //retrieve a page
          File pagina = list.RootFolder.Files.Where(x => x.Name == "Inicio.aspx").First();

          //copyTo method for copy page to siteDestination



i added this code for retrieve the page form the library

                var paginas = clientcontext.Web.Lists.GetByTitle("Pages");

            foreach (File pag in paginas.RootFolder.Files)
               if (pag.Name.ToString() == "Inicio.aspx")
                        //Copy page to another site

i found this code for use the Add method but i dont know how apply it

                   if (pag.Name.ToString() == "Inicio.aspx")
                        var fileCreationInfo = new FileCreationInformation();
                        fileCreationInfo.Url = "";
                        fileCreationInfo.ContentStream = pag.OpenBinaryStream().Value;
                        fileCreationInfo.Content = System.IO.File.ReadAllBytes(pag);
                        fileCreationInfo.Overwrite = true;
                        var uploadFile = Destlist.RootFolder.Files.Add(fileCreationInfo);
  • It's not clear which is the issue you are encountering... Are you getting an error, as described in your question? Or you need a code example about how to copy a file?
    – Cecilia
    Jan 23 '17 at 22:15
  • oh, sorry for this, first i getting the error, but i resolved trying with the update code, now i want to copy the file in another site, thanks you and sorry for that Jan 23 '17 at 22:27


You can use this extension method:

public static Microsoft.SharePoint.Client.File CopyFileTo(this Microsoft.SharePoint.Client.File sourceFile, ClientContext destCtx, string fileDestinationRelativeUrl)

    var sourceContext = sourceFile.Context as ClientContext;

    if (!sourceFile.IsPropertyAvailable("ServerRelativeUrl"))
        sourceContext.Load(sourceFile, f => f.ServerRelativeUrl);

    FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(sourceContext, sourceFile.ServerRelativeUrl);
    Microsoft.SharePoint.Client.File.SaveBinaryDirect(destCtx, fileDestinationRelativeUrl, fileInfo.Stream, true);
    Microsoft.SharePoint.Client.File spfile = destCtx.Web.GetFileByServerRelativeUrl(fileDestinationRelativeUrl);

    return spfile;

And call it like this:

File file = list.RootFolder.Files.Where(x => x.Name == "Inicio.aspx").First();

ListItem srcListItem = file.ListItemAllFields;

var fileLeafRef = file.ListItemAllFields["FileLeafRef"].ToString();
var newRelUrl = ""; // do someting with fileLeafRef --> replace source relative url with dest relative url
var newFile = file.CopyFileTo(clientcontext2, newRelUrl);
  • but the code for enable the feature works, i retrieve the page, only i dont know how copy the page to another site Jan 23 '17 at 22:10
  • Ok, I'll delete my answer because the problem you are encountering is another than the one described in your question...
    – Cecilia
    Jan 23 '17 at 22:12
  • I edited my answer.
    – Cecilia
    Jan 23 '17 at 22:45
  • thanks so much, but im really new in sharepoint add in, and i dont know hot to implement this method, its a provides hosted add id in mvc :/ Jan 23 '17 at 23:40
  • Just add a new static class to your project and insert there the method. Then call it as I described.
    – Cecilia
    Jan 24 '17 at 5:25

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.