Vulnerability in EPiServer.Forms

Try our conversational search powered by Generative AI!

Minesh Shah (Netcel)
Oct 4, 2022
  1351
(2 votes)

Display Child Pages in Content Delivery API Response

The below example will implement an instance of IContentConverterProvider to customise the serialisation of PageData and output child pages in the Content Delivery API response. It is a very simple implementation although can be extended to fully customise the Content Delivery Response.

All code below can be found on the following GIT Repo : https://github.com/Netcel-Optimizely/Optimizely-ContentDelivery-Examples

Firstly let’s create an interface which can be implemented for specific conversion scenarios

    /// <summary>
    /// Content api model property convertor
    /// </summary>
    public interface IContentApiModelConvertor 
    {
        /// <summary>
        /// Convert content to api model
        /// </summary>
        /// <param name="content"></param>
        /// <param name="contentApiModel"></param>
        /// <param name="converterContext"></param>
        void Convert(IContent content, ContentApiModel contentApiModel, ConverterContext converterContext);
    }

Reference : https://github.com/Netcel-Optimizely/Optimizely-ContentDelivery-Examples/blob/main/src/Netcel.ContentDelivery/Interfaces/IContentApiModelConvertor.cs 

To handle the conversion, we must create our own Content Converter for this create a new class called PageContentConvertor which inherits DefaultContentConverter this already implements IContentConverter and deals with the base conversion logic.

    public class PageContentConvertor : DefaultContentConverter
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IEnumerable<IContentApiModelConvertor> _contentApiModelConvertors;

        /// <summary>
        /// Initializes a new instance of the <see cref="PageContentConvertor"/> class.
        /// </summary>
        public PageContentConvertor()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PageContentConvertor"/> class.
        /// Default page convertor
        /// </summary>
        /// <param name="contentTypeRepository"></param>
        /// <param name="reflectionService"></param>
        /// <param name="contentModelService"></param>
        /// <param name="contentVersionRepository"></param>
        /// <param name="contentLoaderService"></param>
        /// <param name="urlResolverService"></param>
        /// <param name="propertyConverterResolver"></param>
        /// <param name="httpContextAccessor"></param>
        /// <param name="contentApiModelConvertors"></param>
        public PageContentConvertor(
            IContentTypeRepository contentTypeRepository,
            ReflectionService reflectionService,
            IContentModelReferenceConverter contentModelService,
            IContentVersionRepository contentVersionRepository,
            ContentLoaderService contentLoaderService,
            UrlResolverService urlResolverService,
            IPropertyConverterResolver propertyConverterResolver,
            IHttpContextAccessor httpContextAccessor,
            IEnumerable<IContentApiModelConvertor> contentApiModelConvertors)
            : base(
                contentTypeRepository,
                reflectionService,
                contentModelService,
                contentVersionRepository,
                contentLoaderService,
                urlResolverService,
                propertyConverterResolver)
        {
            _httpContextAccessor = httpContextAccessor;
            _contentApiModelConvertors = contentApiModelConvertors;
        }

        /// <summary />
        /// <param name="content"></param>
        /// <param name="converterContext"></param>
        /// <returns></returns>
        public override ContentApiModel Convert(IContent content, ConverterContext converterContext)
        {
            if (converterContext.ContextMode.EditOrPreview())
            {
                _httpContextAccessor.HttpContext.SetupVisitorGroupImpersonation(content, AccessLevel.Read);
            }

            var model = base.Convert(content, converterContext);

            foreach (var convertor in _contentApiModelConvertors)
            {
                convertor.Convert(content, model, converterContext);
            }

            if (converterContext.Options.FlattenPropertyModel)
            {
                FlattenPropertyMap(model);
            }

            return model;
        }
    }

Reference : https://github.com/Netcel-Optimizely/Optimizely-ContentDelivery-Examples/blob/main/src/Netcel.ContentDelivery/ContentConvertor/PageContentConvertor.cs

Register this Class at Startup

services.AddSingleton<PageContentConvertor>();

Now, let’s create our own PageContentConvertorProvider which implements IContentConverterProvider

    public class PageContentConvertorProvider : IContentConverterProvider 
    {
        private readonly PageContentConvertor _pageContentConvertor;

        /// <summary>
        /// Initializes a new instance of the <see cref="PageContentConvertorProvider"/> class.
        /// </summary>
        /// <param name="pageContentConvertor"></param>
        public PageContentConvertorProvider(PageContentConvertor pageContentConvertor)
        {
            _pageContentConvertor = pageContentConvertor;
        }

        public int SortOrder => 200;

        /// <summary>
        /// Resolve custom page convertor
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public IContentConverter Resolve(IContent content)
        {
            // further enhance this to allow different supported convertors
            return content is PageData ? _pageContentConvertor : null;
        }
    }

Reference: https://github.com/Netcel-Optimizely/Optimizely-ContentDelivery-Examples/blob/main/src/Netcel.ContentDelivery/ContentConvertorProviders/PageContentConvertorProvider.cs 

We also register at Startup 

 services.TryAddEnumerable(ServiceDescriptor.Scoped<IContentApiModelConvertor, PageDataApiModelConvertor>());

Finally for the actual business logic to perform the conversion we first need a Model for the Navigation Item

    /// <summary>
     /// Navigation Item Dto.
     /// </summary>
    public class NavigationItem
    {
        /// <summary>
        /// Gets or sets navigation Title.
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// Gets or sets navigation Url.
        /// </summary>
        public string Url { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether true if it matches the current page.
        /// </summary>
        public bool IsSelected { get; set; }

        /// <summary>
        /// Is Visibly in Menu Property Set
        /// </summary>
        public bool VisibleInMenu { get; set; }

        public IEnumerable<NavigationItem> Children { get; set; } = Enumerable.Empty<NavigationItem>();
    }

Reference: https://github.com/Netcel-Optimizely/Optimizely-ContentDelivery-Examples/blob/main/src/Netcel.ContentDelivery/Models/NavigationItem.cs 

We than create our Page Model Convertor which contains the business logic to output the Child Pages to Content Delivery API

    /// <summary>
    /// Page Data Content Convertor
    /// </summary>
    public class PageDataApiModelConvertor : IContentApiModelConvertor
    {
        private readonly IContentLoader _contentLoader;
        private readonly IUrlResolver _urlResolver;

        /// <summary>
        /// Initializes a new instance of the <see cref="PageDataApiModelConvertor"/> class.
        /// </summary>
        /// <param name="contentLoader"></param>
        /// <param name="urlResolver"></param>
        public PageDataApiModelConvertor(IContentLoader contentLoader, IUrlResolver urlResolver)
        {
            _contentLoader = contentLoader;
            _urlResolver = urlResolver;
        }

        public void Convert(IContent content, ContentApiModel contentApiModel, ConverterContext converterContext)
        {
            if (content is not PageData pageData)
            {
                return;
            }

            // add navigation
            var navigation = new List<NavigationItem>();
            var children = _contentLoader.GetChildren<PageData>(content.ContentLink);
            navigation.AddRange(children.Select(x => CreateNavigationStructure(x, pageData)));
            contentApiModel.Properties.Add("ChildPages", navigation);


        }

        private NavigationItem CreateNavigationStructure(PageData page, IContent currentContent)
        {
            var model = CreateNavigationItem(page, currentContent);
            var children = _contentLoader.GetChildren<PageData>(page.ContentLink);
            model.Children = children.Select(x => CreateNavigationStructure(x, currentContent));

            return model;
        }

        private NavigationItem CreateNavigationItem(PageData pageContent, IContent currentContent)
        {
            return new NavigationItem
            {
                Title = pageContent.Name,
                Url = _urlResolver.GetUrl(pageContent.ContentLink),
                IsSelected = pageContent.ContentLink == currentContent.ContentLink,
                VisibleInMenu = pageContent.VisibleInMenu,
            };
        }
    }

Reference : https://github.com/Netcel-Optimizely/Optimizely-ContentDelivery-Examples/blob/main/src/Netcel.ContentDelivery/ContentApiModelConvertors/PageDataApiModelConvertor.cs 

Register the conversion provider at Startup 

 services.TryAddEnumerable(ServiceDescriptor.Singleton<IContentConverterProvider, PageContentConvertorProvider>());

If all goes to plan when querying any page via the Content Delivery API the payload of the response should include the child pages.

e.g.

Oct 04, 2022

Comments

Please login to comment.
Latest blogs
Google Read Aloud Reload Problems

Inclusive web experiences greatly benefit from accessibility features such as Google Read Aloud. This tool, which converts text into speech, enable...

Luc Gosso (MVP) | Dec 4, 2023 | Syndicated blog

Import Blobs and Databases to Integration Environments

In this blog, we are going to explore some new extensions to the Deployment API in DXP Cloud Services, specifically the ability to import databases...

Elias Lundmark | Dec 4, 2023

Join the Work Smarter Webinar: Working with the Power of Configured Commerce (B2B) Customer Segmentation December 7th

Join this webinar and learn about customer segmentation – how to best utilize it, how to use personalization to differentiate segmentation and how...

Karen McDougall | Dec 1, 2023

Getting Started with Optimizely SaaS Core and Next.js Integration: Creating Content Pages

The blog post discusses the creation of additional page types with Next.js and Optimizely SaaS Core. It provides a step-by-step guide on how to...

Francisco Quintanilla | Dec 1, 2023 | Syndicated blog

Stop Managing Humans in Your CMS

Too many times, a content management system becomes a people management system. Meaning, an organization uses the CMS to manage all the information...

Deane Barker | Nov 30, 2023

A day in the life of an Optimizely Developer - Optimizely CMS 12: The advantages and considerations when exploring an upgrade

GRAHAM CARR - LEAD .NET DEVELOPER, 28 Nov 2023 In 2022, Optimizely released CMS 12 as part of its ongoing evolution of the platform to help provide...

Graham Carr | Nov 28, 2023