Copy Client-Side Page From One Site To The Other SharePoint Site Using PnP

In this post, I am going to show how to copy a modern page from one site to the other SharePoint site.
 
We can achieve this using PnPOnline and CSOM. This code will copy the modern page into another site collection (destination location) and then, it will also copy all the page contents including web parts and other properties of a modern page to the destination page.
 
A Code sample is provided below, 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Web;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using Microsoft.SharePoint.Client;  
  8. using OfficeDevPnP.Core;  
  9. using OfficeDevPnP.Core.Pages;  
  10. namespace CopyModernPage {  
  11.     class Program {  
  12.         static string sourceSiteUrl = string.Empty;  
  13.         static string sourceUserName = string.Empty;  
  14.         static string sourcePassword = string.Empty;  
  15.         static string destSiteUrl = string.Empty;  
  16.         static string destUserName = string.Empty;  
  17.         static string destPassword = string.Empty;  
  18.         static string pageID = string.Empty;  
  19.         static string listName = string.Empty;  
  20.         static void Main(string[] args) {  
  21.             Console.WriteLine("Please enter source site url:");  
  22.             sourceSiteUrl = Console.ReadLine();  
  23.             Console.WriteLine("Please enter source user name:");  
  24.             sourceUserName = Console.ReadLine();  
  25.             Console.WriteLine("Please enter source password:");  
  26.             sourcePassword = Console.ReadLine();  
  27.             Console.WriteLine("Please enter page library name(for ex: Site Pages):");  
  28.             listName = Console.ReadLine();  
  29.             Console.WriteLine("Please enter source page id:");  
  30.             pageID = Console.ReadLine();  
  31.             Console.WriteLine("Please enter destination site url:");  
  32.             destSiteUrl = Console.ReadLine();  
  33.             Console.WriteLine("Please enter destination user name:");  
  34.             destUserName = Console.ReadLine();  
  35.             Console.WriteLine("Please enter destination password:");  
  36.             destPassword = Console.ReadLine();  
  37.             AuthenticationManager authManager = new AuthenticationManager();  
  38.             try {  
  39.                 using(var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(sourceSiteUrl, sourceUserName, sourcePassword)) {  
  40.                     //listName = "Site Pages";  
  41.                     CamlQuery query = new CamlQuery();  
  42.                     query.ViewXml = "<View><Query>" + "<Where>" + "<Eq><FieldRef Name='ID' /><Value Type='Integer'>" + pageID + "</Value>" + "</Eq>" + "</Where>" + "</Query></View>";  
  43.                     Web srcWeb = clientContext.Web;  
  44.                     clientContext.Load(srcWeb, l => l.Title);  
  45.                     clientContext.ExecuteQuery();  
  46.                     string srcWebTitle = srcWeb.Title;  
  47.                     List list = srcWeb.Lists.GetByTitle(listName);  
  48.                     clientContext.Load(list);  
  49.                     clientContext.ExecuteQuery();  
  50.                     if (list != null) {  
  51.                         ListItemCollection items = list.GetItems(query);  
  52.                         clientContext.Load(items);  
  53.                         clientContext.ExecuteQuery();  
  54.                         if (items != null) {  
  55.                             if (items.Count > 0) {  
  56.                                 foreach(ListItem item in items) {  
  57.                                     Console.WriteLine(item["FileLeafRef"].ToString());  
  58.                                     ClientSidePage sourcePage = clientContext.Web.LoadClientSidePage(item["FileLeafRef"].ToString());  
  59.                                     CopyPageInDestination(sourcePage, item, srcWebTitle);  
  60.                                 }  
  61.                             }  
  62.                         }  
  63.                     } else {  
  64.                         Console.WriteLine("List is not present on the site");  
  65.                     }  
  66.                 }  
  67.             } catch (Exception ex) {  
  68.                 Console.WriteLine("Error: " + ex.Message);  
  69.             }  
  70.             Console.WriteLine("Done!");  
  71.             Console.ReadLine();  
  72.         }  
  73.         private static void CopyPageInDestination(ClientSidePage sourcePage, ListItem sourceItem, string srcWebTitle) {  
  74.             AuthenticationManager authManager = new AuthenticationManager();  
  75.             try {  
  76.                 using(var destClientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(destSiteUrl, destUserName, destPassword)) {  
  77.                     Web destWeb = destClientContext.Web;  
  78.                     destClientContext.Load(destWeb, l => l.Title);  
  79.                     destClientContext.ExecuteQuery();  
  80.                     string destWebTitle = destWeb.Title;  
  81.                     ClientSidePage newPage = destClientContext.Web.AddClientSidePage(sourcePage.PageTitle + ".aspx"true);  
  82.                     if (sourcePage.PageHeader.ImageServerRelativeUrl != null) {  
  83.                         newPage.PageHeader.ImageServerRelativeUrl = sourcePage.PageHeader.ImageServerRelativeUrl.ToLower().Replace(srcWebTitle, destWebTitle);  
  84.                     }  
  85.                     newPage.PageTitle = sourcePage.PageTitle;  
  86.                     newPage.LayoutType = sourcePage.LayoutType;  
  87.                     if (!string.IsNullOrEmpty(Convert.ToString(sourceItem["PromotedState"]))) {  
  88.                         if (Convert.ToInt32(sourceItem["PromotedState"]) == 2) {  
  89.                             newPage.PromoteAsNewsArticle();  
  90.                         }  
  91.                     }  
  92.                     newPage.Save();  
  93.                     ListItem newPageItem = newPage.PageListItem;  
  94.                     newPageItem["CanvasContent1"] = Convert.ToString(sourceItem["CanvasContent1"]).Replace(srcWebTitle, destWebTitle).Replace(srcWebTitle, destWebTitle);  
  95.                     if (sourcePage.LayoutType != ClientSidePageLayoutType.Home) {  
  96.                         newPageItem["PromotedState"] = sourceItem["PromotedState"];  
  97.                     }  
  98.                     newPageItem["PageLayoutType"] = sourceItem["PageLayoutType"];  
  99.                     newPageItem["ClientSideApplicationId"] = sourceItem["ClientSideApplicationId"];  
  100.                     newPageItem["LayoutWebpartsContent"] = sourceItem["LayoutWebpartsContent"];  
  101.                     newPageItem.Update();  
  102.                     destClientContext.ExecuteQuery();  
  103.                     newPage.Publish();  
  104.                 }  
  105.             } catch (Exception ex) {  
  106.                 Console.WriteLine("Error: " + ex.Message);  
  107.             }  
  108.         }  
  109.     }  
  110. }  
Destination Page Library Before Copy
 
Destination Page Library After Copy