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

Ignore Base ContentAPI Model properties from JSON response

Vote:
 

I had this posted in different forum earlier but realized this would get more attention here. I am trying to find a way to ignore base properties of Content API Model .

#224744
Jun 24, 2020 23:09
Vote:
 

Check the documentation below (the CustomContentResultService, to be specific) for instructions on how to remove properties from result:

https://world.episerver.com/documentation/developer-guides/content-delivery-api/how-to-customize-data-returned-to-clients/

#224943
Jun 30, 2020 9:05
Vote:
 

Thank Jorgen, I think the article has some element missing or am not sure if am doing something wrong. I have created class as suggested in article and registered in my initialization module but looks like still the default contentresultservice is executed and not the custom one.

Wondering if anyone has used this can shed some light on it.

#224950
Jun 30, 2020 22:57
Vote:
 

You can customize the ContentResultService as Jørgen mention above, but please note that it only applies to version 2.6.1 and below. For the latest version of Content Delivery Api, you can implement the 'IContentApiModelFilter' and set the base properties to null:

    [ServiceConfiguration(typeof(IContentApiModelFilter), Lifecycle = ServiceInstanceScope.Singleton)]
    public class CustomContentApiModelFilter : ContentApiModelFilter<ContentApiModel>
    {
        public override void Filter(ContentApiModel contentApiModel, ConverterContext converterContext)
        {
            // Set the properties to null here 
        }
    }

Another way is to use the 'select' parameter in the v3 endpoints to filter out properties: https://world.episerver.com/documentation/class-libraries/rest-apis/content-delivery-api/#/ContentApi/ContentApi_GetContent (v3 endpoints still in preview state though).

#224969
Jul 01, 2020 3:33
Vote:
 

Thanks Le.

This is great. I am using version 2.10. However Filter can set the values to null and the properties will still appear in the response with value as null.

Is there a way to completely hide the properties from the response iteslf?

#224986
Jul 01, 2020 17:29
Vote:
 

You can use the IncludeNullValues setting in ContentApiOptions. 

context.Services.Configure<ContentApiConfiguration>(c =>
{    
    c.Default(RestVersion.Version_2_0)
        .SetIncludeNullValues(false);
    
    // if you want to use the v3 endpoints
    c.Default(RestVersion.Version_3_0)
        .SetIncludeNullValues(false);
});
#225017
Jul 02, 2020 7:48
Vote:
 

Le,

Can't thank you enough. For some reason I couldn't find these steps in documentation and was referring to ContentResultService not knowing it is for version under 2.6.

Is there any preferred place to look for such information?

#225031
Jul 02, 2020 20:47
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.