示例#1
0
 /// <summary>
 /// Instantiates a publishing page object
 /// </summary>
 /// <param name="page">ListItem holding the page to analyze</param>
 /// <param name="pageTransformation">Page transformation information</param>
 public PublishingPage(ListItem page, PageTransformation pageTransformation, BaseTransformationInformation baseTransformationInformation, IList <ILogObserver> logObservers = null) : base(page, null, pageTransformation, logObservers)
 {
     // no PublishingPageTransformation specified, fall back to default
     this.publishingPageTransformation  = new PageLayoutManager(base.RegisteredLogObservers).LoadDefaultPageLayoutMappingFile();
     this.baseTransformationInformation = baseTransformationInformation;
     this.functionProcessor             = new PublishingFunctionProcessor(page, cc, null, this.publishingPageTransformation, baseTransformationInformation, base.RegisteredLogObservers);
 }
示例#2
0
 /// <summary>
 /// Instantiates a publishing page object
 /// </summary>
 /// <param name="page">ListItem holding the page to analyze</param>
 /// <param name="pageTransformation">Page transformation information</param>
 public PublishingPage(ListItem page, PageTransformation pageTransformation, PublishingPageTransformation publishingPageTransformation, BaseTransformationInformation baseTransformationInformation, ClientContext targetContext = null, IList <ILogObserver> logObservers = null) : base(page, null, pageTransformation, logObservers)
 {
     this.publishingPageTransformation  = publishingPageTransformation;
     this.baseTransformationInformation = baseTransformationInformation;
     this.targetContext     = targetContext;
     this.functionProcessor = new PublishingFunctionProcessor(page, cc, targetContext, this.publishingPageTransformation, baseTransformationInformation, base.RegisteredLogObservers);
 }
示例#3
0
        private PublishingPageTransformation LoadMappingFile(string mappingFilePath = null)
        {
            // Prepare the result variable
            PublishingPageTransformation result = null;

            if (string.IsNullOrEmpty(mappingFilePath))
            {
                mappingFilePath = "stream.pagelayoutmapping.xml";
            }

            // Check if we already have the mapping file in the in-memory cache
            if (this.memoryCache.TryGetValue(mappingFilePath, out result))
            {
                return(result);
            }

            // Create the xml mapping serializer
            XmlSerializer xmlMapping = new XmlSerializer(typeof(PublishingPageTransformation));

            // If we don't have the mapping file as an input
            if (string.IsNullOrEmpty(mappingFilePath) ||
                !System.IO.File.Exists(mappingFilePath))
            {
                // We use the default one, without validation
                using (var mappingStream = this.GetType().Assembly.GetManifestResourceStream("PnP.Core.Transformation.SharePoint.MappingFiles.pagelayoutmapping.xml"))
                {
                    using (var reader = XmlReader.Create(mappingStream))
                    {
                        result = (PublishingPageTransformation)xmlMapping.Deserialize(reader);
                    }
                }
            }
            else
            {
                using (Stream schema = this.GetType().Assembly.GetManifestResourceStream("PnP.Core.Transformation.SharePoint.MappingFiles.pagelayoutmapping.xsd"))
                {
                    using (var mappingStream = new FileStream(mappingFilePath, FileMode.Open))
                    {
                        // Ensure the provided file complies with the current schema
                        ValidateSchema(schema, mappingStream);

                        using (var reader = XmlReader.Create(mappingStream))
                        {
                            result = (PublishingPageTransformation)xmlMapping.Deserialize(reader);
                        }
                    }
                }
            }

            // Cache the mapping file into the in-memory cache
            this.memoryCache.Set(mappingFilePath, result);

            return(result);
        }
        /// <summary>
        /// Analyse Page Layouts class constructor
        /// </summary>
        public PageLayoutAnalyser(ILogger <PageLayoutAnalyser> logger,
                                  IServiceProvider serviceProvider)
        {
            this.logger          = logger ?? throw new ArgumentNullException(nameof(logger));
            this.serviceProvider = serviceProvider;
            this.memoryCache     = this.serviceProvider.GetService <IMemoryCache>();

            _mapping = new PublishingPageTransformation();
            _contentTypeFieldCache = new Dictionary <string, FieldCollection>();
            _pageLayoutFileCache   = new Dictionary <string, string>();

            parser = new HtmlParser();
        }
 /// <summary>
 /// Instantiates a publishing page object
 /// </summary>
 /// <param name="page">ListItem holding the page to analyze</param>
 /// <param name="pageTransformation">Page transformation information</param>
 public PublishingPageOnPremises(ListItem page, PageTransformation pageTransformation, PublishingPageTransformation publishingPageTransformation, BaseTransformationInformation baseTransformationInformation, ClientContext targetContext = null, IList <ILogObserver> logObservers = null) : base(page, pageTransformation, publishingPageTransformation, baseTransformationInformation, targetContext, logObservers)
 {
 }
 /// <summary>
 /// Instantiates a publishing page object
 /// </summary>
 /// <param name="page">ListItem holding the page to analyze</param>
 /// <param name="pageTransformation">Page transformation information</param>
 public PublishingPage(ListItem page, PageTransformation pageTransformation, PublishingPageTransformation publishingPageTransformation, IList <ILogObserver> logObservers = null) : base(page, pageTransformation, logObservers)
 {
     this.publishingPageTransformation = publishingPageTransformation;
     this.functionProcessor            = new PublishingFunctionProcessor(page, cc, null, this.publishingPageTransformation, base.RegisteredLogObservers);
 }
示例#7
0
        /// <summary>
        /// Maps a classic Page Layout into a modern Page Layout
        /// </summary>
        /// <param name="input">The input for the mapping activity</param>
        /// <param name="token">The cancellation token to use, if any</param>
        /// <returns>The output of the mapping activity</returns>
        public async Task <PageLayoutMappingProviderOutput> MapPageLayoutAsync(PageLayoutMappingProviderInput input, CancellationToken token = default)
        {
            this.taskId = input.Context.Task.Id;

            logger.LogInformation(
                $"Invoked: {this.GetType().Namespace}.{this.GetType().Name}.MapPageLayoutAsync"
                .CorrelateString(this.taskId));

            // Check that we have input data
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            // Validate source item
            var sourceItem = input.Context.SourceItem as SharePointSourceItem;

            if (sourceItem == null)
            {
                throw new ApplicationException(SharePointTransformationResources.Error_MissiningSharePointInputItem);
            }

            // Get a reference to the file and item that we need to transform
            var sourceContext = sourceItem.SourceContext;
            var pageFile      = sourceItem.SourceContext.Web.GetFileByServerRelativeUrl(sourceItem.Id.ServerRelativeUrl);
            var pageItem      = pageFile.ListItemAllFields;

            sourceContext.Load(pageFile);
            sourceContext.Load(pageItem);
            await sourceContext.ExecuteQueryAsync().ConfigureAwait(false);

            // Load the mapping configuration
            PublishingPageTransformation mapping = LoadMappingFile(this.options.Value.PageLayoutMappingFile);

            // Retrieve the mapping page layout from the mapping file, if any
            var publishingPageTransformationModel = mapping.PageLayouts.FirstOrDefault(p => p.Name.Equals(input.PageLayout, StringComparison.InvariantCultureIgnoreCase));

            // No dedicated layout mapping found, let's see if there's an other page layout mapping that also applies for this page layout
            if (publishingPageTransformationModel == null)
            {
                // Fill a list of additional page layout mappings that can be used
                Dictionary <string, string> additionalMappings = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase);
                foreach (var pageLayout in mapping.PageLayouts.Where(p => !String.IsNullOrEmpty(p.AlsoAppliesTo)))
                {
                    var possiblePageLayouts = pageLayout.AlsoAppliesTo.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                    if (possiblePageLayouts.Length > 0)
                    {
                        foreach (var possiblePageLayout in possiblePageLayouts)
                        {
                            // Only add the first possible page layout mapping, if a given page layout is defined multiple times then the first reference wins
                            if (!additionalMappings.ContainsKey(possiblePageLayout))
                            {
                                additionalMappings.Add(possiblePageLayout, pageLayout.Name);
                            }
                        }
                    }
                }

                if (additionalMappings.Count > 0)
                {
                    if (additionalMappings.ContainsKey(input.PageLayout))
                    {
                        publishingPageTransformationModel = mapping.PageLayouts.FirstOrDefault(p => p.Name.Equals(additionalMappings[input.PageLayout], StringComparison.InvariantCultureIgnoreCase));
                    }
                }
            }

            // No layout provided via either the default mapping or custom mapping file provided
            if (publishingPageTransformationModel == null)
            {
                publishingPageTransformationModel = GeneratePageLayout(pageItem);

                logger.LogInformation(
                    SharePointTransformationResources.Info_PageLayoutMappingGeneration
                    .CorrelateString(this.taskId),
                    pageFile.ServerRelativeUrl, input.PageLayout);
            }

            // Still no layout...can't continue...
            if (publishingPageTransformationModel == null)
            {
                var errorMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                 SharePointTransformationResources.Error_NoPageLayoutTransformationModel, input.PageLayout, pageFile.ServerRelativeUrl);
                logger.LogInformation(errorMessage.CorrelateString(this.taskId));
                throw new Exception(errorMessage);
            }

            logger.LogInformation(
                SharePointTransformationResources.Info_PageLayoutMappingBeingUsed
                .CorrelateString(this.taskId),
                pageFile.ServerRelativeUrl, input.PageLayout, publishingPageTransformationModel.Name);

            return(new SharePointPageLayoutMappingProviderOutput {
                PageLayout = publishingPageTransformationModel
            });
        }