Opticon Stockholm is on Tuesday September 10th, hope to see you there!

PuneetGarg
Sep 4, 2024
  156
(4 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

Please login to comment.
Latest blogs
Handling Nynorsk and Bokmål in Optimizely CMS

Warning: Blog post about Norwegian language handling (but might be applicable to other languages and/or use cases). Optimizely have flexible and...

Haakon Peder Haugsten | Sep 5, 2024

Optimizely Headless Form Setup

1. Create empty CMS applications First, let’s setup an empty CMS application. Install the NuGet packages in your solution using the NuGet Package...

Linh Hoang | Sep 4, 2024

Default caching on search request on Search & Navigation

For the better performance, Search & Navigation .Net client has provided StaticallyCacheFor method for caching your search result in a specific of...

Manh Nguyen | Sep 4, 2024

Non-blocking Search with Optimizely Search & Navigation

We are thrilled to announce the integration of asynchronous functions into Optimizely’s Search & Navigation features, which have been supported sin...

Manh Nguyen | Sep 4, 2024

Working with Translations in Optimizely

Ensuring that content is accessible and comprehensible in multiple languages is essential for any global business. Optimizely, combined with Langua...

Luc Gosso (MVP) | Sep 1, 2024 | Syndicated blog