November Happy Hour will be moved to Thursday December 5th.

Render ContentArea in ContentDelivery API

Vote:
 

Hi,

I got an scenario to render ContentArea through Content Delivery API, I'm not sure how we will get an html from ContentArea just to append into the page body.

Any one have any suggestions?

Thank you,

Hari 

#299650
Apr 06, 2023 10:04
Vote:
 

I might be mis-understanding the requirements although adding the expand=* to the page request will also return the content area and the properties of the blocks this includes the HTML if its a Rich Text Editor can you not just pick up what you need and render per your requirements? i.e. 

/api/episerver/v3.0/content/?contentUrl=/cms-testing&expand=*

#299654
Edited, Apr 06, 2023 10:13
Vote:
 

Thanks Minesh for taking time, My scenario is to render the ContentArea, the ContentArea which expected to accept any block type just like our regular CMS PropertyFor Rendering, In this case getting JSON will not be helpful for me because I don't which block type will come.

So my thinking is If I get HTML of complete ContentArea I can append in body. 

#299655
Apr 06, 2023 10:25
Vote:
 

Content Delivery API does render the Block Type as seen in the above Image

Other option would be to modify the output of the ContentArea Property or better still the Block Type via using Property or Content Covertors (IContentApiModelConvertor) 

Business logic to generate the HTML and output into a string property which is also included as part of the Response. 

Here is an example of us picking up some properties from the Homepage and Also outputting it onto all the other Pages, (Not your requirements but something similar can be done, generate the HTML and output as a new property on Page Request) 

    /// <summary>
    /// Master layout content convertor
    /// </summary>
    public class MasterLayoutApiModelConvertor : IContentApiModelConvertor
    {
        private readonly IPropertyConverterResolver _propertyConverterResolver;
        private readonly IContentLoader _contentLoader;
        private readonly IUrlResolver _urlResolver;

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

        /// <summary>
        /// Convert master layout to content api model
        /// </summary>
        /// <param name="content"></param>
        /// <param name="contentApiModel"></param>
        /// <param name="converterContext"></param>
        public void Convert(IContent content, ContentApiModel contentApiModel, ConverterContext converterContext)
        {
            // gets home page
            var homePage = GetHomePage(content);

            if (content is not ISkipHeaderFooterRenderingContent)
            {
                var headerPropertyData = homePage.Property[nameof(HomePage.HeaderContentArea)];
                var footerPropertyData = homePage.Property[nameof(HomePage.FooterContentArea)];

                // adds header area
                AddContentArea(contentApiModel, converterContext, headerPropertyData);

                // adds footer area
                AddContentArea(contentApiModel, converterContext, footerPropertyData);
            }

            if (homePage.NonAuthenticatedRedirectUrl != null)
            {
                contentApiModel.Properties.Add(nameof(HomePage.NonAuthenticatedRedirectUrl), _urlResolver.GetUrl(
                    new UrlBuilder(homePage.NonAuthenticatedRedirectUrl.ToString()), new UrlResolverArguments
                    {
                        ContextMode = ContextMode.Default
                    }));
            }
        }

        private HomePage GetHomePage(IContent content)
        {
            if (content is HomePage homePage)
            {
                return homePage;
            }

            return _contentLoader.Get<HomePage>(ContentReference.StartPage);
        }

        private void AddContentArea(ContentApiModel contentApiModel, ConverterContext converterContext, PropertyData propertyData)
        {
            var propertyConverter = _propertyConverterResolver.Resolve(propertyData);
            if (propertyConverter != null)
            {
                var propertyModel = propertyConverter.Convert(propertyData, converterContext);

                if (propertyModel is ContentAreaPropertyModel contentAreaPropertyModel)
                {
                    contentApiModel.Properties.Add(contentAreaPropertyModel.Name, contentAreaPropertyModel);
                }
            }
        }
    }

 

#299665
Edited, Apr 06, 2023 12:59
Harinarayanan - Apr 26, 2023 13:04
Thnks for the suggestions Minesh, somehow I missed your last reply earlier. I had to change my implementation to regular Page rendering instead of Content Delivery API only for that particular Content Area, becasue the requirement went huge. Thanks again.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.