Articles about European Sharepoint Hosting Service
Sharepoint 2013 Hosting – HostForLIFE.eu :: Sharepoint 2013 Hosting – HostForLIFE.eu ::
In this blog, I am going to perform two actions –
- Identify if a page is a modern page or not.
- Migrate or copy that modern page (including its content) to another site collection or site (destination location)
I am going to accomplish these tasks by using CSOM (Client Object Model). After identifying the modern page, I will copy the modern page into another site collection (destination location) and then, will migrate all the page contents of this modern page (including web parts and other properties of source modern page) to a destination web using CSOM.
We are using a console application to achieve the above functionality using CSOM. In the below-mentioned code logic, we will get all pages from ‘Site Pages’ page library which is present in the source web and looping each page to check if that is a modern page or not.
If the page is found to be a modern page, then we are migrating that page to the destination web ‘Site Pages’ library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
using System; using System.Net; using System.Security; using System.Text.RegularExpressions; using Microsoft.SharePoint.Client; namespace ConsoleApp1 { class Program { static void Main(string[] args) { ClientContext sourceContext = null; List sourceList = null; ListItemCollection srcItemCollection = null; ListItem sourceItem = null; string userName = string.Empty; string password = string.Empty; SecureString srcSiteSecurePassword = new SecureString(); SharePointOnlineCredentials sourceOnlineCredentials = null; try { using(sourceContext = new ClientContext("http://sourcesiteurl")) { userName = "TestUser"; password = "Password"; if (!string.IsNullOrEmpty(password)) { foreach(char c in password.ToCharArray()) srcSiteSecurePassword.AppendChar(c); } // Setting credential for the above site sourceOnlineCredentials = new SharePointOnlineCredentials(userName, srcSiteSecurePassword); sourceContext.Credentials = sourceOnlineCredentials; sourceContext.Load(sourceContext.Web); sourceContext.ExecuteQuery(); // Getting source list by Title sourceList = sourceContext.Web.Lists.GetByTitle("Site Pages"); // Getting all items from source list using caml query srcItemCollection = sourceList.GetItems(CamlQuery.CreateAllItemsQuery()); //Loading source items sourceContext.Load(srcItemCollection, IC => IC.Include(I => I.Id, I => I.DisplayName)); sourceContext.ExecuteQuery(); if (srcItemCollection != null && srcItemCollection.Count > 0) { for (int iCount = 0; iCount < srcItemCollection.Count; iCount++) { try { sourceItem = srcItemCollection[iCount]; //Checking if current page is modern page or not if (IsModernPage(sourceContext, sourceItem)) { // Migrate modern page to anothen site MigrateModernPageToAnotherWeb(sourceContext, sourceItem); } } catch (Exception ex) {} } } } } catch (Exception ex) {} } // Checking if the selected page is a modern page or not private static bool IsModernPage(ClientContext sourceContext, ListItem sourceItem) { bool isModernPage = false; try { sourceContext.Load(sourceItem, srcItm => srcItm["CanvasContent1"], srcItm => srcItm["LayoutWebpartsContent"]); sourceContext.ExecuteQuery(); // Check if modern page if (!string.IsNullOrEmpty(sourceItem["CanvasContent1"].ToString()) || !string.IsNullOrEmpty(sourceItem["LayoutWebpartsContent"].ToString())) isModernPage = true; } catch { isModernPage = false; } return isModernPage; } // Migrating the modern page from source site to destination site private static void MigrateModernPageToAnotherWeb(ClientContext sourceContext, ListItem sourceItem) { ClientContext destSiteContext = null; List destList = null; ListItem destItem = null; string userName = string.Empty; string password = string.Empty; SecureString destSiteSecurePassword = new SecureString(); SharePointOnlineCredentials destOnlineCredentials = null; string canvasContent = string.Empty; string metaInfo = string.Empty; string layoutWebpartsContent = string.Empty; try { using(destSiteContext = new ClientContext("http://destinationsiteurl")) { userName = "TestUser"; password = "Password"; if (!string.IsNullOrEmpty(password)) { foreach(char c in password.ToCharArray()) destSiteSecurePassword.AppendChar(c); } // Setting credential for the above site destOnlineCredentials = new SharePointOnlineCredentials(userName, destSiteSecurePassword); destSiteContext.Credentials = destOnlineCredentials; destSiteContext.Load(destSiteContext.Web); destSiteContext.ExecuteQuery(); // Getting destination list by Title destList = destSiteContext.Web.Lists.GetByTitle("Site Pages"); // Loading destination list destSiteContext.Load(destList, L => L.RootFolder.ServerRelativeUrl); destSiteContext.ExecuteQuery(); // Creating modern page in destination site destItem = destList.RootFolder.Files.AddTemplateFile(destList.RootFolder.ServerRelativeUrl.TrimEnd('/') + "/" + sourceItem.DisplayName + ".aspx", TemplateFileType.ClientSidePage).ListItemAllFields; destSiteContext.Load(destItem); destSiteContext.ExecuteQuery(); // Loading source item properties sourceContext.Load(sourceItem, i => i.ContentType.Id, i => i["CanvasContent1"], i => i["MetaInfo"], i => i["LayoutWebpartsContent"]); sourceContext.ExecuteQuery(); try { destItem["ContentTypeId"] = sourceItem.ContentType.Id.ToString(); canvasContent = sourceItem["CanvasContent1"].ToString(); // Replacing source Web ID with destination Web ID if (!string.IsNullOrEmpty(canvasContent) && canvasContent.Length > 0 && canvasContent.ToLower().Contains(sourceContext.Web.Id.ToString().ToLower())) canvasContent = Regex.Replace(canvasContent, sourceContext.Web.Id.ToString(), destSiteContext.Web.Id.ToString(), RegexOptions.IgnoreCase); } catch (Exception ex) {} try { metaInfo = sourceItem["MetaInfo"].ToString(); // Replacing source Web ID with destination Web ID if (!string.IsNullOrEmpty(metaInfo) && metaInfo.Length > 0 && metaInfo.ToLower().Contains(sourceContext.Web.Id.ToString().ToLower())) metaInfo = Regex.Replace(metaInfo, sourceContext.Web.Id.ToString(), destSiteContext.Web.Id.ToString(), RegexOptions.IgnoreCase); } catch (Exception ex) {} try { layoutWebpartsContent = sourceItem["LayoutWebpartsContent"].ToString(); // Replacing source Web ID with destination Web ID if (!string.IsNullOrEmpty(layoutWebpartsContent) && layoutWebpartsContent.Length > 0 && layoutWebpartsContent.ToLower().Contains(sourceContext.Web.Id.ToString().ToLower())) layoutWebpartsContent = Regex.Replace(layoutWebpartsContent, sourceContext.Web.Id.ToString(), destSiteContext.Web.Id.ToString(), RegexOptions.IgnoreCase); } catch (Exception ex) {} // Setting source page canvas content to destination page if (!string.IsNullOrEmpty(canvasContent) && canvasContent.Length > 0) destItem["CanvasContent1"] = canvasContent; // Setting source page metaInfo content to destination page if (!string.IsNullOrEmpty(metaInfo) && metaInfo.Length > 0) destItem["MetaInfo"] = metaInfo; // Setting source page layout webparts content to destination page if (!string.IsNullOrEmpty(layoutWebpartsContent) && layoutWebpartsContent.Length > 0) destItem["LayoutWebpartsContent"] = layoutWebpartsContent; // Updating the destination page destItem.Update(); destSiteContext.ExecuteQuery(); } } catch (Exception ex) {} } } } |
SharePoint 2013 Hosting Recommendation
HostForLIFE.eu’s SharePoint 2013 Hosting solution offers a comprehensive feature set that is easy-to-use for new users, yet powerful enough for the most demanding web developer expert. Hosted SharePoint Foundation 2013 is the premiere web-based collaboration and productivity enhancement tool on the market today. With SharePoint 2013 Foundation, you can quickly access and manage documents and information anytime, anywhere though a Web browser in a secure and user friendly way. SharePoint hosting services start at only at €9.99/mo, allowing you to take advantage of the robust feature set for a small business price. HostForLIFE.eu offers a variety of hosted SharePoint Foundation 2013 plans as well as dedicated SharePoint 2013 Foundation options
Print article | This entry was posted by Peter on January 10, 2019 at 7:48 am, and is filed under Other Related Post. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |