Join us this Friday for AI in Action at the Virtual Happy Hour! This free virtual event is open to all—enroll now on Academy and don’t miss out.

 

What is the best way to add new properties to the response of the content delivery api?

Vote:
 

Hi all, 

Currently I am looking into customizing the response of the content delivery api by adding new properties.

For now I was able to achieve something of that sort by overriding the ContentConvertingService class.

I would like to know if there is a better approach to add custom properties to the content delivery api.

public class CustomContentConvertingService: ContentConvertingService
{

    public CustomContentConvertingService(IContentConverterResolver contentConverterResolver, IEnumerable<IContentFilter> contentFilters, IEnumerable<IContentApiModelFilter> contentApiModelFilters)
        : base(contentConverterResolver, contentFilters, contentApiModelFilters)
    {
    }
    public override ContentApiModel ConvertToContentApiModel(IContent content, ConverterContext converterContext)
    {
        var data = base.ConvertToContentApiModel(content, converterContext);

        data.Properties.Add("NewProperty", "NewPropertyData");

        return data;
    }

}
#317086
Feb 13, 2024 9:29
Vote:
 

We implement IContentApiModelFilter in a more simple fasion, allowing us to perform better SOC in the end, see https://docs.developers.optimizely.com/content-management-system/v1.5.0-content-delivery-api/docs/how-to-customize-data-returned-to-clients

This will in it's most simple form look something like this

public class PageBaseFilter : IContentApiModelFilter
{
    public void Filter(ContentApiModel contentApiModel, ConverterContext converterContext)
    {
        if (contentApiModel.ContentLink != null)
        {
            contentApiModel.Properties.Add("Id", contentApiModel.ContentLink.GuidValue.ToString());
        }
        // more more more 
    }
}
#317099
Feb 13, 2024 20:41
* 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.