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

PuneetGarg
Sep 4, 2024
  384
(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
Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog

I'm running Optimizely CMS on .NET 9!

It works 🎉

Tomas Hensrud Gulla | Nov 12, 2024 | Syndicated blog

Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog

ExcludeDeleted(): Prevent Trashed Content from Appearing in Search Results

Introduction In Optimizely CMS, content that is moved to the trash can still appear in search results if it’s not explicitly excluded using the...

Ashish Rasal | Nov 7, 2024

CMS + CMP + Graph integration

We have just released a new package https://nuget.optimizely.com/package/?id=EPiServer.Cms.WelcomeIntegration.Graph which changes the way CMS fetch...

Bartosz Sekula | Nov 5, 2024

Block type selection doesn't work

Imagine you're trying to create a new block in a specific content area. You click the "Create" link, expecting to see a CMS modal with a list of...

Damian Smutek | Nov 4, 2024 | Syndicated blog