public PublishingMetadataTransformator(PublishingPageTransformationInformation publishingPageTransformationInformation, ClientContext sourceClientContext, ClientContext targetClientContext, ClientSidePage page, PageLayout publishingPageLayoutModel, PublishingPageTransformation publishingPageTransformation, IList <ILogObserver> logObservers = null)
        {
            // Register observers
            if (logObservers != null)
            {
                foreach (var observer in logObservers)
                {
                    base.RegisterObserver(observer);
                }
            }

            this.publishingPageTransformationInformation = publishingPageTransformationInformation;
            this.targetClientContext = targetClientContext;
            this.page = page;
            this.pageLayoutMappingModel       = publishingPageLayoutModel;
            this.publishingPageTransformation = publishingPageTransformation;
            this.functionProcessor            = new PublishingFunctionProcessor(publishingPageTransformationInformation.SourcePage, sourceClientContext, targetClientContext, this.publishingPageTransformation, publishingPageTransformationInformation as BaseTransformationInformation, base.RegisteredLogObservers);
        }
        public PublishingMetadataTransformator(PublishingPageTransformationInformation publishingPageTransformationInformation, ClientContext sourceClientContext, ClientContext targetClientContext, ClientSidePage page, PageLayout publishingPageLayoutModel, IList <ILogObserver> logObservers = null)
        {
            // Register observers
            if (logObservers != null)
            {
                foreach (var observer in logObservers)
                {
                    base.RegisterObserver(observer);
                }
            }

            this.publishingPageTransformationInformation = publishingPageTransformationInformation;
            this.sourceClientContext = sourceClientContext;
            this.targetClientContext = targetClientContext;
            this.page = page;
            this.pageLayoutMappingModel = publishingPageLayoutModel;
        }
        /// <summary>
        /// Analyses a single page layout from a provided file
        /// </summary>
        /// <param name="pageLayoutItem">Page layout list item</param>
        public PageLayout AnalysePageLayout(ListItem pageLayoutItem)
        {
            try
            {
                // Get the associated page layout content type
                string assocContentType      = pageLayoutItem[Constants.PublishingAssociatedContentTypeField].ToString();
                var    assocContentTypeParts = assocContentType.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);

                // Load content type fields in memory once
                var contentTypeFields = LoadContentTypeFields(assocContentTypeParts[1]);

                // Extact page header
                var extractedHeader = ExtractPageHeaderFromPageLayoutAssociatedContentType(contentTypeFields);

                // Analyze the pagelayout file content
                var extractedHtmlBlocks = ExtractControlsFromPageLayoutHtml(pageLayoutItem);
                extractedHtmlBlocks.WebPartFields = CleanExtractedWebPartFields(extractedHtmlBlocks.WebPartFields, contentTypeFields);

                // Detect the fields that will become metadata in the target site
                var extractedMetaDataFields = ExtractMetaDataFromPageLayoutAssociatedContentType(contentTypeFields, extractedHtmlBlocks.WebPartFields, extractedHeader);

                var metaData = new MetaData
                {
                    Field = extractedMetaDataFields
                };

                // Combine all data to a single PageLayout mapping
                var layoutMapping = new PageLayout()
                {
                    // Display name of the page layout
                    Name = Path.GetFileNameWithoutExtension(pageLayoutItem[Constants.FileLeafRefField].ToString()),
                    // Default to no page header for now
                    PageHeader = extractedHeader != null ? PageLayoutPageHeader.Custom : PageLayoutPageHeader.None,
                    // Default to autodetect layout model
                    PageLayoutTemplate = PageLayoutPageLayoutTemplate.AutoDetect,
                    // The content type to be used on the target modern page
                    AssociatedContentType = "",
                    // Set the header details (if any)
                    Header = extractedHeader,
                    // Fields that will become metadata fields for the target page
                    MetaData = metaData,
                    // Fields that will become web parts on the target page
                    WebParts = extractedHtmlBlocks.WebPartFields.Count > 0 ? extractedHtmlBlocks.WebPartFields.ToArray() : null,
                    // Web part zones that can hold zero or more web parts
                    WebPartZones = extractedHtmlBlocks.WebPartZones.Count > 0 ? extractedHtmlBlocks.WebPartZones.ToArray() : null,
                    // Fixed web parts, this are web parts which are 'hardcoded' in the pagelayout aspx file
                    FixedWebParts = extractedHtmlBlocks.FixedWebParts.Count > 0 ? extractedHtmlBlocks.FixedWebParts?.ToArray() : null,
                };

                // Add to mappings list
                if (_mapping.PageLayouts != null)
                {
                    var expandMappings = _mapping.PageLayouts.ToList();

                    // Prevent duplicate references to the same page layout
                    if (!expandMappings.Any(o => o.Name == layoutMapping.Name))
                    {
                        expandMappings.Add(layoutMapping);
                    }

                    _mapping.PageLayouts = expandMappings.ToArray();
                }
                else
                {
                    _mapping.PageLayouts = new[] { layoutMapping };
                }

                LogInfo(string.Format(LogStrings.AnalyserMappingLayout, layoutMapping.Name), LogStrings.Heading_PageLayoutAnalyser);

                return(layoutMapping);
            }
            catch (Exception ex)
            {
                LogError(LogStrings.Error_CannotProcessPageLayoutAnalyse, LogStrings.Heading_PageLayoutAnalyser, ex);
            }

            return(null);
        }
Esempio n. 4
0
 private static HeaderField GetHeaderField(PageLayout publishingPageTransformationModel, HeaderFieldHeaderProperty fieldName)
 {
     return(publishingPageTransformationModel.Header.Field.Where(p => p.HeaderProperty == fieldName).FirstOrDefault());
 }