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

Please login to comment.
Latest blogs
SEO redirects in .NET + Optimizely

Nice and easy way to add necessary SEO redirects

Damian Smutek | Oct 14, 2024 | Syndicated blog

Multiple Anonymous Carts created from external Head front fetching custom Api

Scenario and Problem Working in a custom headless architecture where a NextJs application hosted in Vercel consumes a custom API built in a...

David Ortiz | Oct 11, 2024

Content Search with Optimizely Graph

Optimizely Graph lets you fetch content and sync data from other Optimizely products. For content search, this lets you create custom search tools...

Dileep D | Oct 9, 2024 | Syndicated blog

Omnichannel Analytics Simplified – Optimizely Acquires Netspring

Recently, the news broke that Optimizely acquired Netspring, a warehouse-native analytics platform. I’ll admit, I hadn’t heard of Netspring before,...

Alex Harris - Perficient | Oct 9, 2024 | Syndicated blog

Problem with language file localization after upgrading to Optimizely CMS 12

Avoid common problems with xml file localization when upgrading from Optimizely CMS 11 to CMS 12.

Tomas Hensrud Gulla | Oct 9, 2024 | Syndicated blog

Optimizely Autocomplete (Statistics)

A user starts typing in the search input, and it returns suggestions for phrases they might be searching for. How to achieve this?

Damian Smutek | Oct 9, 2024 | Syndicated blog