/// <summary> /// Transform the publishing page /// </summary> /// <param name="publishingPageTransformationInformation">Information about the publishing page to transform</param> /// <returns>The path to the created modern page</returns> public string Transform(PublishingPageTransformationInformation publishingPageTransformationInformation) { SetPageId(Guid.NewGuid().ToString()); var logsForSettings = this.DetailSettingsAsLogEntries(publishingPageTransformationInformation); logsForSettings?.ForEach(o => Log(o, LogLevel.Information)); #region Input validation if (publishingPageTransformationInformation.SourcePage == null) { LogError(LogStrings.Error_SourcePageNotFound, LogStrings.Heading_InputValidation); throw new ArgumentNullException(LogStrings.Error_SourcePageNotFound); } // Validate page and it's eligibility for transformation if (!publishingPageTransformationInformation.SourcePage.FieldExistsAndUsed(Constants.FileRefField) || !publishingPageTransformationInformation.SourcePage.FieldExistsAndUsed(Constants.FileLeafRefField)) { LogError(LogStrings.Error_PageNotValidMissingFileRef, LogStrings.Heading_InputValidation); throw new ArgumentException(LogStrings.Error_PageNotValidMissingFileRef); } string pageType = publishingPageTransformationInformation.SourcePage.PageType(); if (pageType.Equals("ClientSidePage", StringComparison.InvariantCultureIgnoreCase)) { LogError(LogStrings.Error_SourcePageIsModern, LogStrings.Heading_InputValidation); throw new ArgumentException(LogStrings.Error_SourcePageIsModern); } if (pageType.Equals("AspxPage", StringComparison.InvariantCultureIgnoreCase)) { LogError(LogStrings.Error_BasicASPXPageCannotTransform, LogStrings.Heading_InputValidation); throw new ArgumentException(LogStrings.Error_BasicASPXPageCannotTransform); } if (pageType.Equals("WikiPage", StringComparison.InvariantCultureIgnoreCase) || pageType.Equals("WebPartPage", StringComparison.InvariantCultureIgnoreCase)) { LogError(LogStrings.Error_PageIsNotAPublishingPage, LogStrings.Heading_InputValidation); throw new ArgumentException(LogStrings.Error_PageIsNotAPublishingPage); } // Disable cross-farm item level permissions from copying CrossFarmTransformationValidation(publishingPageTransformationInformation); LogDebug(LogStrings.ValidationChecksComplete, LogStrings.Heading_InputValidation); #endregion try { #region Telemetry #if DEBUG && MEASURE Start(); #endif DateTime transformationStartDateTime = DateTime.Now; LogDebug(LogStrings.LoadingClientContextObjects, LogStrings.Heading_SharePointConnection); LoadClientObject(sourceClientContext); LogInfo($"{sourceClientContext.Web.Url}", LogStrings.Heading_Summary, LogEntrySignificance.SourceSiteUrl); LogDebug(LogStrings.LoadingTargetClientContext, LogStrings.Heading_SharePointConnection); LoadClientObject(targetClientContext); if (sourceClientContext.Site.Id.Equals(targetClientContext.Site.Id)) { // Oops, seems source and target point to the same site collection...that's a no go for publishing portal page transformation! LogError(LogStrings.Error_SameSiteTransferNoAllowedForPublishingPages, LogStrings.Heading_SharePointConnection); throw new ArgumentNullException(LogStrings.Error_SameSiteTransferNoAllowedForPublishingPages); } LogInfo($"{targetClientContext.Web.Url}", LogStrings.Heading_Summary, LogEntrySignificance.TargetSiteUrl); // Need to add further validation for target template if (targetClientContext.Web.WebTemplate != "SITEPAGEPUBLISHING" && targetClientContext.Web.WebTemplate != "STS" && targetClientContext.Web.WebTemplate != "GROUP") { LogError(LogStrings.Error_CrossSiteTransferTargetsNonModernSite); throw new ArgumentException(LogStrings.Error_CrossSiteTransferTargetsNonModernSite, LogStrings.Heading_SharePointConnection); } LogInfo($"{publishingPageTransformationInformation.SourcePage[Constants.FileRefField].ToString().ToLower()}", LogStrings.Heading_Summary, LogEntrySignificance.SourcePage); #if DEBUG && MEASURE Stop("Telemetry"); #endif #endregion #region Page creation // Detect if the page is living inside a folder LogDebug(LogStrings.DetectIfPageIsInFolder, LogStrings.Heading_PageCreation); string pageFolder = ""; // Get the publishing pages library name this.publishingPagesLibrary = CacheManager.Instance.GetPublishingPagesLibraryName(this.sourceClientContext); if (publishingPageTransformationInformation.SourcePage.FieldExistsAndUsed(Constants.FileDirRefField)) { var fileRefFieldValue = publishingPageTransformationInformation.SourcePage[Constants.FileDirRefField].ToString().ToLower(); if (fileRefFieldValue.Contains($"/{this.publishingPagesLibrary}")) { string pagesLibraryRelativeUrl = $"{sourceClientContext.Web.ServerRelativeUrl.TrimEnd(new[] { '/' })}/{this.publishingPagesLibrary}"; pageFolder = fileRefFieldValue.Replace(pagesLibraryRelativeUrl.ToLower(), "").Trim(); } else { // Page was living in another list, leave the list name as that will be the folder hosting the modern file in SitePages. // This convention is used to avoid naming conflicts pageFolder = fileRefFieldValue.Replace($"{sourceClientContext.Web.ServerRelativeUrl}", "").Trim(); } if (pageFolder.Length > 0) { if (pageFolder.Contains("/")) { if (pageFolder == "/") { pageFolder = ""; } else { pageFolder = pageFolder.Substring(1); } } // Add a trailing slash pageFolder = pageFolder + "/"; LogInfo(LogStrings.PageIsLocatedInFolder, LogStrings.Heading_PageCreation); } } publishingPageTransformationInformation.Folder = pageFolder; // If no targetname specified then we'll come up with one if (string.IsNullOrEmpty(publishingPageTransformationInformation.TargetPageName)) { LogInfo(LogStrings.CrossSiteInUseUsingOriginalFileName, LogStrings.Heading_PageCreation); publishingPageTransformationInformation.TargetPageName = $"{publishingPageTransformationInformation.SourcePage[Constants.FileLeafRefField].ToString()}"; } // Check if page name is free to use #if DEBUG && MEASURE Start(); #endif bool pageExists = false; ClientSidePage targetPage = null; List pagesLibrary = null; Microsoft.SharePoint.Client.File existingFile = null; //The determines of the target client context has been specified and use that to generate the target page var context = targetClientContext; try { LogDebug(LogStrings.LoadingExistingPageIfExists, LogStrings.Heading_PageCreation); // Just try to load the page in the fastest possible manner, we only want to see if the page exists or not existingFile = Load(sourceClientContext, targetClientContext, publishingPageTransformationInformation, out pagesLibrary); pageExists = true; } catch (Exception ex) { if (ex is ArgumentException) { //Non-critical error generated LogInfo(LogStrings.CheckPageExistsError, LogStrings.Heading_PageCreation); } else { //Something else occurred LogError(LogStrings.CheckPageExistsError, LogStrings.Heading_PageCreation, ex); } } #if DEBUG && MEASURE Stop("Load Page"); #endif if (pageExists) { LogInfo(LogStrings.PageAlreadyExistsInTargetLocation, LogStrings.Heading_PageCreation); if (!publishingPageTransformationInformation.Overwrite) { var message = $"{LogStrings.PageNotOverwriteIfExists} {publishingPageTransformationInformation.TargetPageName}."; LogError(message, LogStrings.Heading_PageCreation); throw new ArgumentException(message); } } // Create the client side page targetPage = context.Web.AddClientSidePage($"{publishingPageTransformationInformation.Folder}{publishingPageTransformationInformation.TargetPageName}"); LogInfo($"{LogStrings.ModernPageCreated} ", LogStrings.Heading_PageCreation); #endregion LogInfo(LogStrings.TransformSourcePageAsArticlePage, LogStrings.Heading_ArticlePageHandling); #region Analysis of the source page #if DEBUG && MEASURE Start(); #endif // Analyze the source page Tuple <Pages.PageLayout, List <WebPartEntity> > pageData = null; LogInfo($"{LogStrings.TransformSourcePageIsPublishingPage} - {LogStrings.TransformSourcePageAnalysing}", LogStrings.Heading_ArticlePageHandling); // Grab the pagelayout mapping to use: var pageLayoutMappingModel = GetPageLayoutMappingModel(publishingPageTransformationInformation.SourcePage); pageData = new PublishingPage(publishingPageTransformationInformation.SourcePage, pageTransformation, this.publishingPageTransformation, publishingPageTransformationInformation as BaseTransformationInformation, targetContext: targetClientContext, logObservers: base.RegisteredLogObservers).Analyze(pageLayoutMappingModel); // Wiki content can contain embedded images and videos, which is not supported by the target RTE...split wiki text blocks so the transformator can handle the images and videos as separate web parts LogInfo(LogStrings.WikiTextContainsImagesVideosReferences, LogStrings.Heading_ArticlePageHandling); pageData = new Tuple <Pages.PageLayout, List <WebPartEntity> >(pageData.Item1, new WikiHtmlTransformator(this.sourceClientContext, targetPage, publishingPageTransformationInformation as BaseTransformationInformation, base.RegisteredLogObservers).TransformPlusSplit(pageData.Item2, publishingPageTransformationInformation.HandleWikiImagesAndVideos)); #if DEBUG && MEASURE Stop("Analyze page"); #endif #endregion #region Page title configuration #if DEBUG && MEASURE Start(); #endif // Set page title SetPageTitle(publishingPageTransformationInformation, targetPage); if (publishingPageTransformationInformation.PageTitleOverride != null) { var title = publishingPageTransformationInformation.PageTitleOverride(targetPage.PageTitle); targetPage.PageTitle = title; LogInfo($"{LogStrings.TransformPageTitleOverride} - page title: {title}", LogStrings.Heading_ArticlePageHandling); } #if DEBUG && MEASURE Stop("Set page title"); #endif #endregion #region Page layout configuration #if DEBUG && MEASURE Start(); #endif // Use the default layout transformator ILayoutTransformator layoutTransformator = new LayoutTransformator(targetPage); // Do we have an override? bool useCustomLayoutTransformator = false; if (publishingPageTransformationInformation.LayoutTransformatorOverride != null) { LogInfo(LogStrings.TransformLayoutTransformatorOverride, LogStrings.Heading_ArticlePageHandling); layoutTransformator = publishingPageTransformationInformation.LayoutTransformatorOverride(targetPage); useCustomLayoutTransformator = true; } // Apply the layout to the page layoutTransformator.Transform(pageData); // If needed call the specific publishing page layout transformator if (pageData.Item1 == Pages.PageLayout.PublishingPage_AutoDetect && !useCustomLayoutTransformator) { // Call out the specific publishing layout transformator implementation PublishingLayoutTransformator publishingLayoutTransformator = new PublishingLayoutTransformator(targetPage, base.RegisteredLogObservers); publishingLayoutTransformator.Transform(pageData); } #if DEBUG && MEASURE Stop("Page layout"); #endif #endregion #region Content transformation LogDebug(LogStrings.PreparingContentTransformation, LogStrings.Heading_ArticlePageHandling); #if DEBUG && MEASURE Start(); #endif // Use the default content transformator IContentTransformator contentTransformator = new ContentTransformator(sourceClientContext, targetPage, pageTransformation, publishingPageTransformationInformation as BaseTransformationInformation, base.RegisteredLogObservers); // Do we have an override? if (publishingPageTransformationInformation.ContentTransformatorOverride != null) { LogInfo(LogStrings.TransformUsingContentTransformerOverride, LogStrings.Heading_ArticlePageHandling); contentTransformator = publishingPageTransformationInformation.ContentTransformatorOverride(targetPage, pageTransformation); } LogInfo(LogStrings.TransformingContentStart, LogStrings.Heading_ArticlePageHandling); // Run the content transformator contentTransformator.Transform(pageData.Item2); LogInfo(LogStrings.TransformingContentEnd, LogStrings.Heading_ArticlePageHandling); #if DEBUG && MEASURE Stop("Content transformation"); #endif #endregion #region Configure header for target page #if DEBUG && MEASURE Start(); #endif PublishingPageHeaderTransformator headerTransformator = new PublishingPageHeaderTransformator(publishingPageTransformationInformation, sourceClientContext, targetClientContext, this.publishingPageTransformation, base.RegisteredLogObservers); headerTransformator.TransformHeader(ref targetPage); #if DEBUG && MEASURE Stop("Target page header"); #endif #endregion #region Text/Section/Column cleanup // Drop "empty" text parts. Wiki pages tend to have a lot of text parts just containing div's and BR's...no point in keep those as they generate to much whitespace RemoveEmptyTextParts(targetPage); // Remove empty sections and columns to optimize screen real estate if (publishingPageTransformationInformation.RemoveEmptySectionsAndColumns) { RemoveEmptySectionsAndColumns(targetPage); } #endregion #region Page persisting + permissions #region Save the page #if DEBUG && MEASURE Start(); #endif // Persist the client side page var pageName = $"{publishingPageTransformationInformation.Folder}{publishingPageTransformationInformation.TargetPageName}"; targetPage.Save(pageName); LogInfo($"{LogStrings.TransformSavedPageInCrossSiteCollection}: {pageName}", LogStrings.Heading_ArticlePageHandling); // Tag the file with a page modernization version stamp string serverRelativePathForModernPage = ReturnModernPageServerRelativeUrl(publishingPageTransformationInformation); try { var targetPageFile = context.Web.GetFileByServerRelativeUrl(serverRelativePathForModernPage); context.Load(targetPageFile, p => p.Properties); targetPageFile.Properties["sharepointpnp_pagemodernization"] = this.version; targetPageFile.Update(); if (publishingPageTransformationInformation.PublishCreatedPage) { // Try to publish, if publish is not needed then this will return an error that we'll be ignoring targetPageFile.Publish("Page modernization initial publish"); } // Send both the property update and publish as a single operation to SharePoint context.ExecuteQueryRetry(); } catch (Exception ex) { // Eat exceptions as this is not critical for the generated page LogWarning(LogStrings.Warning_NonCriticalErrorDuringVersionStampAndPublish, LogStrings.Heading_ArticlePageHandling); } // Disable page comments on the create page, if needed if (publishingPageTransformationInformation.DisablePageComments) { targetPage.DisableComments(); LogInfo(LogStrings.TransformDisablePageComments, LogStrings.Heading_ArticlePageHandling); } #if DEBUG && MEASURE Stop("Persist page"); #endif #endregion #region Page metadata handling PublishingMetadataTransformator publishingMetadataTransformator = new PublishingMetadataTransformator(publishingPageTransformationInformation, sourceClientContext, targetClientContext, targetPage, pageLayoutMappingModel, this.publishingPageTransformation, base.RegisteredLogObservers); publishingMetadataTransformator.Transform(); #endregion #region Permission handling ListItemPermission listItemPermissionsToKeep = null; if (publishingPageTransformationInformation.KeepPageSpecificPermissions) { #if DEBUG && MEASURE Start(); #endif // Check if we do have item level permissions we want to take over listItemPermissionsToKeep = GetItemLevelPermissions(true, pagesLibrary, publishingPageTransformationInformation.SourcePage, targetPage.PageListItem); // When creating the page in another site collection we'll always want to copy item level permissions if specified ApplyItemLevelPermissions(true, targetPage.PageListItem, listItemPermissionsToKeep); #if DEBUG && MEASURE Stop("Permission handling"); #endif } #endregion #region Telemetry if (!publishingPageTransformationInformation.SkipTelemetry && this.pageTelemetry != null) { TimeSpan duration = DateTime.Now.Subtract(transformationStartDateTime); this.pageTelemetry.LogTransformationDone(duration, pageType, publishingPageTransformationInformation); this.pageTelemetry.Flush(); } LogInfo(LogStrings.TransformComplete, LogStrings.Heading_PageCreation); #endregion #region Closing CacheManager.Instance.SetLastUsedTransformator(this); return(serverRelativePathForModernPage); #endregion #endregion } catch (Exception ex) { LogError(LogStrings.CriticalError_ErrorOccurred, LogStrings.Heading_Summary, ex, isCriticalException: true); // Throw exception if there's no registered log observers if (base.RegisteredLogObservers.Count == 0) { throw; } } return(string.Empty); }
/// <summary> /// Transform the publishing page /// </summary> /// <param name="publishingPageTransformationInformation">Information about the publishing page to transform</param> /// <returns>The path to the created modern page</returns> public string Transform(PublishingPageTransformationInformation publishingPageTransformationInformation) { SetPageId(Guid.NewGuid().ToString()); var logsForSettings = this.DetailSettingsAsLogEntries(publishingPageTransformationInformation); logsForSettings?.ForEach(o => Log(o, LogLevel.Information)); // Have this ensure to ensure system.text.json is included - needed by OfficeDevPnP var jsonLoad = System.Text.Json.JsonSerializer.Serialize(new List <string>() { "something " }); #region Input validation if (publishingPageTransformationInformation.SourcePage == null) { LogError(LogStrings.Error_SourcePageNotFound, LogStrings.Heading_InputValidation); throw new ArgumentNullException(LogStrings.Error_SourcePageNotFound); } // Validate page and it's eligibility for transformation if (!publishingPageTransformationInformation.SourcePage.FieldExistsAndUsed(Constants.FileRefField) || !publishingPageTransformationInformation.SourcePage.FieldExistsAndUsed(Constants.FileLeafRefField)) { LogError(LogStrings.Error_PageNotValidMissingFileRef, LogStrings.Heading_InputValidation); throw new ArgumentException(LogStrings.Error_PageNotValidMissingFileRef); } string pageType = publishingPageTransformationInformation.SourcePage.PageType(); if (pageType.Equals("ClientSidePage", StringComparison.InvariantCultureIgnoreCase)) { LogError(LogStrings.Error_SourcePageIsModern, LogStrings.Heading_InputValidation); throw new ArgumentException(LogStrings.Error_SourcePageIsModern); } if (pageType.Equals("AspxPage", StringComparison.InvariantCultureIgnoreCase)) { LogError(LogStrings.Error_BasicASPXPageCannotTransform, LogStrings.Heading_InputValidation); throw new ArgumentException(LogStrings.Error_BasicASPXPageCannotTransform); } if (pageType.Equals("WikiPage", StringComparison.InvariantCultureIgnoreCase) || pageType.Equals("WebPartPage", StringComparison.InvariantCultureIgnoreCase)) { LogError(LogStrings.Error_PageIsNotAPublishingPage, LogStrings.Heading_InputValidation); throw new ArgumentException(LogStrings.Error_PageIsNotAPublishingPage); } if (IsDelveBlogPage(pageType)) { LogError(LogStrings.Error_DelveBlogPagesNotSupported, LogStrings.Heading_InputValidation); throw new ArgumentException(LogStrings.Error_DelveBlogPagesNotSupported); } // Disable cross-farm item level permissions from copying CrossFarmTransformationValidation(publishingPageTransformationInformation); LogDebug(LogStrings.ValidationChecksComplete, LogStrings.Heading_InputValidation); #endregion try { #region Telemetry #if DEBUG && MEASURE Start(); #endif DateTime transformationStartDateTime = DateTime.Now; LogDebug(LogStrings.LoadingClientContextObjects, LogStrings.Heading_SharePointConnection); LoadClientObject(sourceClientContext, false); LogInfo($"{sourceClientContext.Web.GetUrl()}", LogStrings.Heading_Summary, LogEntrySignificance.SourceSiteUrl); LogDebug(LogStrings.LoadingTargetClientContext, LogStrings.Heading_SharePointConnection); LoadClientObject(targetClientContext, true); PopulateGlobalProperties(sourceClientContext, targetClientContext); if (sourceClientContext.Site.Id.Equals(targetClientContext.Site.Id)) { // Oops, seems source and target point to the same site collection...that's a no go for publishing portal page transformation! LogError(LogStrings.Error_SameSiteTransferNoAllowedForPublishingPages, LogStrings.Heading_SharePointConnection); throw new ArgumentNullException(LogStrings.Error_SameSiteTransferNoAllowedForPublishingPages); } LogInfo($"{targetClientContext.Web.GetUrl()}", LogStrings.Heading_Summary, LogEntrySignificance.TargetSiteUrl); // Need to add further validation for target template if (targetClientContext.Web.WebTemplate != "SITEPAGEPUBLISHING" && targetClientContext.Web.WebTemplate != "STS" && targetClientContext.Web.WebTemplate != "GROUP" && targetClientContext.Web.WebTemplate != "BDR" && targetClientContext.Web.WebTemplate != "DEV") { LogError(LogStrings.Error_CrossSiteTransferTargetsNonModernSite); throw new ArgumentException(LogStrings.Error_CrossSiteTransferTargetsNonModernSite, LogStrings.Heading_SharePointConnection); } // Ensure PostAsNews is used together with PagePublishing if (publishingPageTransformationInformation.PostAsNews && !publishingPageTransformationInformation.PublishCreatedPage) { publishingPageTransformationInformation.PublishCreatedPage = true; LogWarning(LogStrings.Warning_PostingAPageAsNewsRequiresPagePublishing, LogStrings.Heading_Summary); } // Mark this web as publishing web CacheManager.Instance.SetPublishingWeb(this.sourceClientContext.Web.GetUrl()); // Store the information of the source page we do want to retain if (publishingPageTransformationInformation.KeepPageCreationModificationInformation) { StoreSourcePageInformationToKeep(publishingPageTransformationInformation.SourcePage); } LogInfo($"{publishingPageTransformationInformation.SourcePage[Constants.FileRefField].ToString()}", LogStrings.Heading_Summary, LogEntrySignificance.SourcePage); var spVersion = publishingPageTransformationInformation.SourceVersion; var exactSpVersion = publishingPageTransformationInformation.SourceVersionNumber; LogInfo($"{spVersion.DisplaySharePointVersion()} ({exactSpVersion})", LogStrings.Heading_Summary, LogEntrySignificance.SharePointVersion); LogInfo(LogStrings.TransformationModePublishing, LogStrings.Heading_Summary, LogEntrySignificance.TransformMode); //Load User Mapping File InitializeUserMapping(publishingPageTransformationInformation); #if DEBUG && MEASURE Stop("Telemetry"); #endif #endregion #region Page creation // Detect if the page is living inside a folder LogDebug(LogStrings.DetectIfPageIsInFolder, LogStrings.Heading_PageCreation); string pageFolder = ""; // Get the publishing pages library name this.publishingPagesLibraryName = CacheManager.Instance.GetPublishingPagesLibraryName(this.sourceClientContext); if (publishingPageTransformationInformation.SourcePage.FieldExistsAndUsed(Constants.FileDirRefField)) { var fileRefFieldValue = publishingPageTransformationInformation.SourcePage[Constants.FileDirRefField].ToString(); if (fileRefFieldValue.ContainsIgnoringCasing($"/{this.publishingPagesLibraryName}")) { string pagesLibraryRelativeUrl = $"{sourceClientContext.Web.ServerRelativeUrl.TrimEnd(new[] { '/' })}/{this.publishingPagesLibraryName}"; pageFolder = fileRefFieldValue.Replace(pagesLibraryRelativeUrl, "", StringComparison.InvariantCultureIgnoreCase).Trim(); } else { // Page was living in another list, leave the list name as that will be the folder hosting the modern file in SitePages. // This convention is used to avoid naming conflicts pageFolder = fileRefFieldValue.Replace($"{sourceClientContext.Web.ServerRelativeUrl}", "").Trim(); } if (pageFolder.Length > 0 || !string.IsNullOrEmpty(publishingPageTransformationInformation.TargetPageFolder)) { if (pageFolder.StartsWith("/")) { if (pageFolder == "/") { pageFolder = ""; } else { pageFolder = pageFolder.Substring(1); } } // Add a trailing slash pageFolder = pageFolder + "/"; if (!string.IsNullOrEmpty(publishingPageTransformationInformation.TargetPageFolder)) { // Handle special case <root> to indicate page should be created in the root folder if (publishingPageTransformationInformation.TargetPageFolder.Equals("<root>", StringComparison.InvariantCultureIgnoreCase)) { publishingPageTransformationInformation.TargetPageFolder = ""; } if (publishingPageTransformationInformation.TargetPageFolderOverridesDefaultFolder) { pageFolder = publishingPageTransformationInformation.TargetPageFolder; } else { pageFolder = Path.Combine(pageFolder, publishingPageTransformationInformation.TargetPageFolder); } if (!pageFolder.EndsWith("/")) { // Add a trailing slash pageFolder = pageFolder + "/"; } } LogInfo(LogStrings.PageIsLocatedInFolder, LogStrings.Heading_PageCreation); } } publishingPageTransformationInformation.Folder = pageFolder; // If no targetname specified then we'll come up with one if (string.IsNullOrEmpty(publishingPageTransformationInformation.TargetPageName)) { LogInfo(LogStrings.CrossSiteInUseUsingOriginalFileName, LogStrings.Heading_PageCreation); publishingPageTransformationInformation.TargetPageName = $"{publishingPageTransformationInformation.SourcePage[Constants.FileLeafRefField].ToString()}"; } // Check if page name is free to use #if DEBUG && MEASURE Start(); #endif bool pageExists = false; ClientSidePage targetPage = null; List pagesLibrary = null; Microsoft.SharePoint.Client.File existingFile = null; //The determines of the target client context has been specified and use that to generate the target page var context = targetClientContext; try { LogDebug(LogStrings.LoadingExistingPageIfExists, LogStrings.Heading_PageCreation); // Just try to load the page in the fastest possible manner, we only want to see if the page exists or not existingFile = Load(sourceClientContext, targetClientContext, publishingPageTransformationInformation, out pagesLibrary); pageExists = true; } catch (Exception ex) { if (ex is ArgumentException) { //Non-critical error generated LogInfo(LogStrings.CheckPageExistsError, LogStrings.Heading_PageCreation); } else { //Something else occurred LogError(LogStrings.CheckPageExistsError, LogStrings.Heading_PageCreation, ex); } } #if DEBUG && MEASURE Stop("Load Page"); #endif if (pageExists) { LogInfo(LogStrings.PageAlreadyExistsInTargetLocation, LogStrings.Heading_PageCreation); if (!publishingPageTransformationInformation.Overwrite) { var message = $"{LogStrings.PageNotOverwriteIfExists} {publishingPageTransformationInformation.TargetPageName}."; LogError(message, LogStrings.Heading_PageCreation); throw new ArgumentException(message); } } // Create the client side page targetPage = context.Web.AddClientSidePage($"{publishingPageTransformationInformation.Folder}{publishingPageTransformationInformation.TargetPageName}"); LogInfo($"{LogStrings.ModernPageCreated} ", LogStrings.Heading_PageCreation); #endregion LogInfo(LogStrings.TransformSourcePageAsArticlePage, LogStrings.Heading_ArticlePageHandling); #region Analysis of the source page #if DEBUG && MEASURE Start(); #endif // Analyze the source page Tuple <Pages.PageLayout, List <WebPartEntity> > pageData = null; LogInfo($"{LogStrings.TransformSourcePageIsPublishingPage} - {LogStrings.TransformSourcePageAnalysing}", LogStrings.Heading_ArticlePageHandling); // Grab the pagelayout mapping to use: var pageLayoutMappingModel = new PageLayoutManager(this.RegisteredLogObservers).GetPageLayoutMappingModel(this.publishingPageTransformation, publishingPageTransformationInformation.SourcePage); if (spVersion == SPVersion.SP2010 || spVersion == SPVersion.SP2013Legacy || spVersion == SPVersion.SP2016Legacy) { pageData = new PublishingPageOnPremises(publishingPageTransformationInformation.SourcePage, pageTransformation, this.publishingPageTransformation, publishingPageTransformationInformation as BaseTransformationInformation, targetContext: targetClientContext, logObservers: base.RegisteredLogObservers).Analyze(pageLayoutMappingModel); } else { pageData = new PublishingPage(publishingPageTransformationInformation.SourcePage, pageTransformation, this.publishingPageTransformation, publishingPageTransformationInformation as BaseTransformationInformation, targetContext: targetClientContext, logObservers: base.RegisteredLogObservers).Analyze(pageLayoutMappingModel); } // Wiki content can contain embedded images and videos, which is not supported by the target RTE...split wiki text blocks so the transformator can handle the images and videos as separate web parts LogInfo(LogStrings.WikiTextContainsImagesVideosReferences, LogStrings.Heading_ArticlePageHandling); pageData = new Tuple <Pages.PageLayout, List <WebPartEntity> >(pageData.Item1, new WikiHtmlTransformator(this.sourceClientContext, targetPage, publishingPageTransformationInformation as BaseTransformationInformation, base.RegisteredLogObservers).TransformPlusSplit(pageData.Item2, publishingPageTransformationInformation.HandleWikiImagesAndVideos, publishingPageTransformationInformation.AddTableListImageAsImageWebPart)); #if DEBUG && MEASURE Stop("Analyze page"); #endif #endregion #region Page title configuration #if DEBUG && MEASURE Start(); #endif // Set page title SetPageTitle(publishingPageTransformationInformation, targetPage); if (publishingPageTransformationInformation.PageTitleOverride != null) { var title = publishingPageTransformationInformation.PageTitleOverride(targetPage.PageTitle); targetPage.PageTitle = title; LogInfo($"{LogStrings.TransformPageTitleOverride} - page title: {title}", LogStrings.Heading_ArticlePageHandling); } #if DEBUG && MEASURE Stop("Set page title"); #endif #endregion #region Page layout configuration #if DEBUG && MEASURE Start(); #endif // Use the default layout transformator ILayoutTransformator layoutTransformator = new LayoutTransformator(targetPage); // Do we have an override? bool useCustomLayoutTransformator = false; if (publishingPageTransformationInformation.LayoutTransformatorOverride != null) { LogInfo(LogStrings.TransformLayoutTransformatorOverride, LogStrings.Heading_ArticlePageHandling); layoutTransformator = publishingPageTransformationInformation.LayoutTransformatorOverride(targetPage); useCustomLayoutTransformator = true; } // Apply the layout to the page layoutTransformator.Transform(pageData); // If needed call the specific publishing page layout transformator if ((pageData.Item1 == Pages.PageLayout.PublishingPage_AutoDetect || pageData.Item1 == Pages.PageLayout.PublishingPage_AutoDetectWithVerticalColumn) && !useCustomLayoutTransformator) { // Call out the specific publishing layout transformator implementation PublishingLayoutTransformator publishingLayoutTransformator = new PublishingLayoutTransformator(targetPage, pageLayoutMappingModel, base.RegisteredLogObservers); publishingLayoutTransformator.Transform(pageData); } #if DEBUG && MEASURE Stop("Page layout"); #endif #endregion #region Content transformation LogDebug(LogStrings.PreparingContentTransformation, LogStrings.Heading_ArticlePageHandling); #if DEBUG && MEASURE Start(); #endif // Use the default content transformator IContentTransformator contentTransformator = new ContentTransformator(sourceClientContext, targetPage, pageTransformation, publishingPageTransformationInformation as BaseTransformationInformation, base.RegisteredLogObservers); // Do we have an override? if (publishingPageTransformationInformation.ContentTransformatorOverride != null) { LogInfo(LogStrings.TransformUsingContentTransformerOverride, LogStrings.Heading_ArticlePageHandling); contentTransformator = publishingPageTransformationInformation.ContentTransformatorOverride(targetPage, pageTransformation); } LogInfo(LogStrings.TransformingContentStart, LogStrings.Heading_ArticlePageHandling); // Run the content transformator contentTransformator.Transform(pageData.Item2.Where(c => !c.IsClosed).ToList()); LogInfo(LogStrings.TransformingContentEnd, LogStrings.Heading_ArticlePageHandling); #if DEBUG && MEASURE Stop("Content transformation"); #endif #endregion #region Configure header for target page #if DEBUG && MEASURE Start(); #endif PublishingPageHeaderTransformator headerTransformator = new PublishingPageHeaderTransformator(publishingPageTransformationInformation, sourceClientContext, targetClientContext, this.publishingPageTransformation, base.RegisteredLogObservers); headerTransformator.TransformHeader(ref targetPage); #if DEBUG && MEASURE Stop("Target page header"); #endif #endregion #region Text/Section/Column cleanup // Drop "empty" text parts. Wiki pages tend to have a lot of text parts just containing div's and BR's...no point in keep those as they generate to much whitespace RemoveEmptyTextParts(targetPage); // Remove empty sections and columns to optimize screen real estate if (publishingPageTransformationInformation.RemoveEmptySectionsAndColumns) { RemoveEmptySectionsAndColumns(targetPage); } #endregion #region Page persisting + permissions #region Save the page #if DEBUG && MEASURE Start(); #endif // Persist the client side page var pageName = $"{publishingPageTransformationInformation.Folder}{publishingPageTransformationInformation.TargetPageName}"; targetPage.Save(pageName); LogInfo($"{LogStrings.TransformSavedPageInCrossSiteCollection}: {pageName}", LogStrings.Heading_ArticlePageHandling); #if DEBUG && MEASURE Stop("Persist page"); #endif #endregion #region Page metadata handling PublishingMetadataTransformator publishingMetadataTransformator = new PublishingMetadataTransformator(publishingPageTransformationInformation, sourceClientContext, targetClientContext, targetPage, pageLayoutMappingModel, this.publishingPageTransformation, this.userTransformator, base.RegisteredLogObservers); publishingMetadataTransformator.Transform(); #endregion #region Permission handling ListItemPermission listItemPermissionsToKeep = null; if (publishingPageTransformationInformation.KeepPageSpecificPermissions) { #if DEBUG && MEASURE Start(); #endif // Check if we do have item level permissions we want to take over listItemPermissionsToKeep = GetItemLevelPermissions(true, pagesLibrary, publishingPageTransformationInformation.SourcePage, targetPage.PageListItem); // When creating the page in another site collection we'll always want to copy item level permissions if specified ApplyItemLevelPermissions(true, targetPage.PageListItem, listItemPermissionsToKeep); #if DEBUG && MEASURE Stop("Permission handling"); #endif } #endregion #region Page Publishing // Tag the file with a page modernization version stamp string serverRelativePathForModernPage = targetPage.PageListItem[Constants.FileRefField].ToString(); bool pageListItemWasReloaded = false; try { var targetPageFile = context.Web.GetFileByServerRelativeUrl(serverRelativePathForModernPage); context.Load(targetPageFile, p => p.Properties); targetPageFile.Properties["sharepointpnp_pagemodernization"] = this.version; targetPageFile.Update(); // In case of KeepPageCreationModificationInformation and PostAsNews the publishing is handled at the very end of the flow, so skip it right here if (!publishingPageTransformationInformation.KeepPageCreationModificationInformation && !publishingPageTransformationInformation.PostAsNews && publishingPageTransformationInformation.PublishCreatedPage) { // Try to publish, if publish is not needed/possible (e.g. when no minor/major versioning set) then this will return an error that we'll be ignoring targetPageFile.Publish(LogStrings.PublishMessage); } // Ensure we've the most recent page list item loaded, must be last statement before calling ExecuteQuery context.Load(targetPage.PageListItem); // Send both the property update and publish as a single operation to SharePoint context.ExecuteQueryRetry(); pageListItemWasReloaded = true; } catch (Exception ex) { // Eat exceptions as this is not critical for the generated page LogWarning(LogStrings.Warning_NonCriticalErrorDuringVersionStampAndPublish, LogStrings.Heading_ArticlePageHandling); } // Update flags field to indicate this is a "migrated" page try { // If for some reason the reload batched with the previous request did not finish then do it again if (!pageListItemWasReloaded) { context.Load(targetPage.PageListItem); context.ExecuteQueryRetry(); } // Only perform the update when the field was not yet set bool skipSettingMigratedFromServerRendered = false; if (targetPage.PageListItem[Constants.SPSitePageFlagsField] != null) { skipSettingMigratedFromServerRendered = (targetPage.PageListItem[Constants.SPSitePageFlagsField] as string[]).Contains("MigratedFromServerRendered"); } if (!skipSettingMigratedFromServerRendered) { targetPage.PageListItem[Constants.SPSitePageFlagsField] = ";#MigratedFromServerRendered;#"; targetPage.PageListItem.UpdateOverwriteVersion(); context.Load(targetPage.PageListItem); context.ExecuteQueryRetry(); } } catch (Exception ex) { // Eat any exception } // Disable page comments on the create page, if needed if (publishingPageTransformationInformation.DisablePageComments) { targetPage.DisableComments(); LogInfo(LogStrings.TransformDisablePageComments, LogStrings.Heading_ArticlePageHandling); } #endregion #region Restore page author/editor/created/modified if ((publishingPageTransformationInformation.KeepPageCreationModificationInformation && this.SourcePageAuthor != null && this.SourcePageEditor != null) || publishingPageTransformationInformation.PostAsNews) { UpdateTargetPageWithSourcePageInformation(targetPage.PageListItem, publishingPageTransformationInformation, serverRelativePathForModernPage, true); } #endregion // NO page updates are allowed anymore past this point as otherwise the set page usage information and published/posted state will be impacted! #region Telemetry if (!publishingPageTransformationInformation.SkipTelemetry && this.pageTelemetry != null) { TimeSpan duration = DateTime.Now.Subtract(transformationStartDateTime); this.pageTelemetry.LogTransformationDone(duration, pageType, publishingPageTransformationInformation); this.pageTelemetry.Flush(); } LogInfo(LogStrings.TransformComplete, LogStrings.Heading_PageCreation); #endregion #region Closing CacheManager.Instance.SetLastUsedTransformator(this); LogInfo($"{serverRelativePathForModernPage}", LogStrings.Heading_Summary, LogEntrySignificance.TargetPage); return(Uri.EscapeUriString(serverRelativePathForModernPage)); #endregion #endregion } catch (Exception ex) { LogError(LogStrings.CriticalError_ErrorOccurred, LogStrings.Heading_Summary, ex, isCriticalException: true); // Throw exception if there's no registered log observers if (base.RegisteredLogObservers.Count == 0) { throw; } } return(string.Empty); }