Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.

 

PuneetGarg
Sep 4, 2024
  454
(5 votes)

Remove Unwanted properties for Headless Implementation using Content Delivery API

While working with Headless, whenever we want to send data to the front end, many properties are also shown in JSON that we don't wish to, which creates a big and unreadable response.

To refactor that we can create Filter which will kick before and after every call.

Example:-

PreSerialization Content filter 

  • This class prevents the serialization of certain properties before the serialization
  • Register that Service as Singleton 
internal class PreSerializationContentFilter  : ContentFilter<IContent>
{
    public override void Filter(IContent content, ConverterContext converterContext)
    {
        content.Property.Remove("TopContentArea");
        content.Property.Remove("MainContentArea");
        content.Property.Remove("BottomContentArea");

        content.Property.Remove("MetaTitle");
        content.Property.Remove("MetaKeywords");
        content.Property.Remove("MetaDescription");
        content.Property.Remove("DisableIndexing");
        content.Property.Remove("EnableNoFollow");
        content.Property.Remove("OgContentType");
        content.Property.Remove("Categories");
    }
}

PostSerialization ApiModelFilter

  • This generic class adds properties to the ContentApiModel after the serialization before sending the request
  • Register that Service as Singleton 
 public class PostSerializationApiModelFilter : ContentApiModelFilter<ContentApiModel>
 {
     public override void Filter(ContentApiModel contentApiModel, ConverterContext converterContext)
     {
         try
         {
             // Set those values below as null, and configure ContentApiOption.IncludeNullValues = false in Initialization
             // then, response data will not include those ones.
             contentApiModel.ContentLink = null;
             contentApiModel.Language = null;
             contentApiModel.ExistingLanguages = null;
             contentApiModel.MasterLanguage = null;
             contentApiModel.ParentLink = null;
             contentApiModel.StartPublish = null;
             contentApiModel.StopPublish = null;
             contentApiModel.RouteSegment = null;
             contentApiModel.Changed = null;
             contentApiModel.Created = null;
             contentApiModel.Saved = null;
             contentApiModel.Status = null;
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message);
         }
     }
 }

Using these classes you can remove unwanted properties.

Hope that helps !! :-)  

Sep 04, 2024

Comments

Harinarayanan
Harinarayanan Oct 3, 2024 10:53 AM

Nice post, Thanks for sharing.

Ola Andersson
Ola Andersson Jan 10, 2025 10:28 AM

Hi! I can not get this to work with version 3.11.3 of the ContentDeliveryApi beacuse IncludeNullValues is marked as obsolete and casting an error if you try to change it. Is there any solution to this?

PuneetGarg
PuneetGarg Jan 10, 2025 03:24 PM

Hi @ola , use this instead options.IncludeEmptyContentProperties = false;

Please login to comment.
Latest blogs
Level Up with Optimizely's Newly Relaunched Certifications!

We're thrilled to announce the relaunch of our Optimizely Certifications—designed to help partners, customers, and developers redefine what it mean...

Satata Satez | Jan 14, 2025

Introducing AI Assistance for DBLocalizationProvider

The LocalizationProvider for Optimizely has long been a powerful tool for enhancing the localization capabilities of Optimizely CMS. Designed to ma...

Luc Gosso (MVP) | Jan 14, 2025 | Syndicated blog

Order tabs with drag and drop - Blazor

I have started to play around a little with Blazor and the best way to learn is to reimplement some old stuff for CMS12. So I took a look at my old...

Per Nergård | Jan 14, 2025

Product Recommendations - Common Pitfalls

With the added freedom and flexibility that the release of the self-service widgets feature for Product Recommendations provides you as...

Dylan Walker | Jan 14, 2025