{"id":5238,"date":"2019-08-09T10:15:08","date_gmt":"2019-08-09T10:15:08","guid":{"rendered":"https:\/\/www.softreetechnology.com\/?p=5238"},"modified":"2019-08-09T10:15:08","modified_gmt":"2019-08-09T10:15:08","slug":"copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp","status":"publish","type":"post","link":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp\/","title":{"rendered":"Copy Client-Side Page from one site to the other SharePoint site using PnP"},"content":{"rendered":"\n<p>In this post, I am\ngoing to show how to copy a modern page from one site to the other SharePoint\nsite.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p><strong><span style=\"text-decoration: underline;\">A Code sample is provided below:<\/span><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\nusing System.Web;\nusing System.Linq;\nusing System.Text;\nusing System.Threading.Tasks;\nusing Microsoft.SharePoint.Client;\nusing OfficeDevPnP.Core;\nusing OfficeDevPnP.Core.Pages;\n\nnamespace CopyModernPage\n{\n    class Program\n    {\n        static string sourceSiteUrl = string.Empty;\n        static string sourceUserName = string.Empty;\n        static string sourcePassword = string.Empty;\n        static string destSiteUrl = string.Empty;\n        static string destUserName = string.Empty;\n        static string destPassword = string.Empty;\n        static string pageID = string.Empty;\n        static string listName = string.Empty;\n        static void Main(string[] args)\n        {\n            Console.WriteLine(\"Please enter source site url:\");\n            sourceSiteUrl = Console.ReadLine();\n            Console.WriteLine(\"Please enter source user name:\");\n            sourceUserName = Console.ReadLine();\n            Console.WriteLine(\"Please enter source password:\");\n            sourcePassword = Console.ReadLine();\n            Console.WriteLine(\"Please enter page library name(for ex: Site Pages):\");\n            listName = Console.ReadLine();\n            Console.WriteLine(\"Please enter source page id:\");\n            pageID = Console.ReadLine();\n            Console.WriteLine(\"Please enter destination site url:\");\n            destSiteUrl = Console.ReadLine();\n            Console.WriteLine(\"Please enter destination user name:\");\n            destUserName = Console.ReadLine();\n            Console.WriteLine(\"Please enter destination password:\");\n            destPassword = Console.ReadLine();\n\n            AuthenticationManager authManager = new AuthenticationManager();\n\n            try\n            {\n                using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(sourceSiteUrl, sourceUserName, sourcePassword))\n                {\n                    \/\/listName = \"Site Pages\";\n                    CamlQuery query = new CamlQuery();\n                    query.ViewXml = \"&lt;View>&lt;Query>\"\n                                        + \"&lt;Where>\"\n                                            + \"&lt;Eq>&lt;FieldRef Name='ID' \/>&lt;Value Type='Integer'>\" + pageID + \"&lt;\/Value>\" +\n                                            \"&lt;\/Eq>\" +\n                                        \"&lt;\/Where>\" +\n                                \"&lt;\/Query>&lt;\/View>\";\n\n                    Web srcWeb = clientContext.Web;\n                    clientContext.Load(srcWeb, l => l.Title);\n                    clientContext.ExecuteQuery();\n                    string srcWebTitle = srcWeb.Title;\n                    List list = srcWeb.Lists.GetByTitle(listName);\n                    clientContext.Load(list);\n                    clientContext.ExecuteQuery();\n\n                    if (list != null)\n                    {\n                        ListItemCollection items = list.GetItems(query);\n                        clientContext.Load(items);\n                        clientContext.ExecuteQuery();\n\n                        if (items != null)\n                        {\n                            if (items.Count > 0)\n                            {\n                                foreach (ListItem item in items)\n                                {\n                                    Console.WriteLine(item[\"FileLeafRef\"].ToString());\n                                    ClientSidePage sourcePage = clientContext.Web.LoadClientSidePage(item[\"FileLeafRef\"].ToString());\n                                    \n                                    CopyPageInDestination(sourcePage, item, srcWebTitle);\n                                }\n                            }\n                        }\n                    }\n                    else\n                    {\n                        Console.WriteLine(\"List is not present on the site\");\n                    }\n                }\n            }\n            catch (Exception ex)\n            {\n                Console.WriteLine(\"Error: \" + ex.Message);\n            }\n            \n            Console.WriteLine(\"Done!\");\n            Console.ReadLine();\n        }\n\n        private static void CopyPageInDestination(ClientSidePage sourcePage, ListItem sourceItem, string srcWebTitle)\n        {\n            AuthenticationManager authManager = new AuthenticationManager();\n\n            try\n            {\n                using (var destClientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(destSiteUrl, destUserName, destPassword))\n                {\n                    Web destWeb = destClientContext.Web;\n                    destClientContext.Load(destWeb, l => l.Title);\n                    destClientContext.ExecuteQuery();\n                    string destWebTitle = destWeb.Title;\n                    ClientSidePage newPage = destClientContext.Web.AddClientSidePage(sourcePage.PageTitle+\".aspx\", true);\n\n                    if (sourcePage.PageHeader.ImageServerRelativeUrl != null)\n                    {\n                        newPage.PageHeader.ImageServerRelativeUrl = sourcePage.PageHeader.ImageServerRelativeUrl.ToLower().Replace(srcWebTitle, destWebTitle);\n                    }\n                    newPage.PageTitle = sourcePage.PageTitle;\n                    newPage.LayoutType = sourcePage.LayoutType;\n                    \n                    if (!string.IsNullOrEmpty(Convert.ToString(sourceItem[\"PromotedState\"])))\n                    {\n                        if (Convert.ToInt32(sourceItem[\"PromotedState\"]) == 2)\n                        {\n                            newPage.PromoteAsNewsArticle();\n                        }\n                    }\n                    newPage.Save();\n\n\n                    ListItem newPageItem = newPage.PageListItem;\n                    newPageItem[\"CanvasContent1\"] = Convert.ToString(sourceItem[\"CanvasContent1\"]).Replace(srcWebTitle, destWebTitle).Replace(srcWebTitle, destWebTitle);\n                    if (sourcePage.LayoutType != ClientSidePageLayoutType.Home)\n                    {\n                        newPageItem[\"PromotedState\"] = sourceItem[\"PromotedState\"];\n                    }\n                    newPageItem[\"PageLayoutType\"] = sourceItem[\"PageLayoutType\"];\n                    newPageItem[\"ClientSideApplicationId\"] = sourceItem[\"ClientSideApplicationId\"];\n                    newPageItem[\"LayoutWebpartsContent\"] = sourceItem[\"LayoutWebpartsContent\"];\n                    newPageItem.Update();\n                    destClientContext.ExecuteQuery();\n\n                    newPage.Publish();\n                }\n            }\n            catch (Exception ex)\n            {\n                Console.WriteLine(\"Error: \" + ex.Message);\n            }\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><span style=\"text-decoration: underline;\">OutPut:<\/span><\/p>\n\n\n\n<p><a>Destination Page Library Before Copy<\/a><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.softreetechnology.com\/wp-content\/uploads\/2019\/08\/Destination-page-library-before-copy.png\" alt=\"\" class=\"wp-image-5239\"\/><\/figure><\/div>\n\n\n\n<p><strong>Destination Page Library After Copy<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.softreetechnology.com\/wp-content\/uploads\/2019\/08\/Destination-page-library-after-Copy.png\" alt=\"\" class=\"wp-image-5240\"\/><\/figure>\n\n\n\n<p><strong>This solution is brought to you by our SharePoint professionals.<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.softreetechnology.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Softree Technology<\/a>&nbsp;employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.<\/p>\n\n\n\n<p>Be it&nbsp;SPFx or SharePoint&nbsp;add-in developments,&nbsp;<a href=\"https:\/\/www.softreetechnology.com\/sharepoint-2019-developments\" target=\"_blank\" rel=\"noreferrer noopener\">SharePoint 2019 developments,<\/a>&nbsp;web part developments, migrating from SharePoint 2010\/2013 to SharePoint 2013\/2016\/Office 365, Office 365,&nbsp;SharePoint hosted apps developmen<a href=\"http:\/\/www.softreeconsulting.com\/sharepoint-azure-ad\/\">t<\/a>&nbsp;or something else in SharePoint, we strive to deliver the best<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5241,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[270,271,272,273,274,275],"class_list":["post-5238","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sharepoint","tag-copy-client-side-page-using-csom","tag-copy-client-side-page-using-pnp","tag-copy-modern-site-page-using-csom","tag-copy-modern-site-page-using-pnp","tag-pnp-online","tag-sharepoint-modern-site-page"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Copy Client-Side Page from one site to the other SharePoint site using PnP - Softree Technology<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Copy Client-Side Page from one site to the other SharePoint site using PnP - Softree Technology\" \/>\n<meta property=\"og:description\" content=\"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 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp\/\" \/>\n<meta property=\"og:site_name\" content=\"Softree Technology\" \/>\n<meta property=\"article:published_time\" content=\"2019-08-09T10:15:08+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Copy Client-Side Page from one site to the other SharePoint site using PnP - Softree Technology","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp\/","og_locale":"en_US","og_type":"article","og_title":"Copy Client-Side Page from one site to the other SharePoint site using PnP - Softree Technology","og_description":"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 [&hellip;]","og_url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp\/","og_site_name":"Softree Technology","article_published_time":"2019-08-09T10:15:08+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp\/#article","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp\/"},"author":{"name":"admin","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b"},"headline":"Copy Client-Side Page from one site to the other SharePoint site using PnP","datePublished":"2019-08-09T10:15:08+00:00","mainEntityOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp\/"},"wordCount":188,"commentCount":0,"publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp\/#primaryimage"},"thumbnailUrl":"","keywords":["copy client-side page using csom","copy client-side page using pnp","copy modern site page using csom","copy modern site page using pnp","pnp online","sharepoint modern site page"],"articleSection":["SharePoint"],"inLanguage":"en","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp\/","url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp\/","name":"Copy Client-Side Page from one site to the other SharePoint site using PnP - Softree Technology","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp\/#primaryimage"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp\/#primaryimage"},"thumbnailUrl":"","datePublished":"2019-08-09T10:15:08+00:00","breadcrumb":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp\/"]}]},{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copy-client-side-page-from-one-site-to-the-other-sharepoint-site-using-pnp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/softreetechnology.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Copy Client-Side Page from one site to the other SharePoint site using PnP"}]},{"@type":"WebSite","@id":"https:\/\/softreetechnology.com\/blog\/#website","url":"https:\/\/softreetechnology.com\/blog\/","name":"Softree Technology","description":"Celebrating 10+ Years in SharePoint Consulting !","publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/softreetechnology.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en"},{"@type":"Organization","@id":"https:\/\/softreetechnology.com\/blog\/#organization","name":"Softree Technology","url":"https:\/\/softreetechnology.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","contentUrl":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","width":844,"height":230,"caption":"Softree Technology"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b","name":"admin","image":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/softreeconsulting.com"],"url":"https:\/\/softreetechnology.com\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/5238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=5238"}],"version-history":[{"count":0,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/5238\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=5238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=5238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=5238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}