Try our conversational search powered by Generative AI!

Content Delivery API: Exposing derived properties

Vote:
 

Hi,

We have a page type with two properties: Heading and NavigationHeading, where NavigationHeading defaults to the value of Heading if NavigationHeading is empty (as seen below).

[ContentType(DisplayName = "MyPageType", GUID = "4c19e432-905d-46d7-bd16-ea8754a4e268", Description = "")]
    public class MyPageType : PageData
    {
        [CultureSpecific]
        [Display(
            Name = "Heading",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 1)]
        public virtual string Heading { get; set; }

        [CultureSpecific]
        [Display(
            Name = "NavigationHeading",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 2)]
        public virtual string NavigationHeading
        {
            get
            {
                var navigationHeading = this.GetPropertyValue(page => page.NavigationHeading);

                return string.IsNullOrWhiteSpace(navigationHeading) ? Heading : navigationHeading;
            }
            set
            {
                this.SetPropertyValue(page => page.NavigationHeading, value);
            }
        }
    }

When retrieving this page through the Content Delivery API, both properties are exposed. However, the logic expressed for NavigationHeading is not run when the page is serialized. 

The table below illustrates the behaviour of the Content Delivery API for the page type above.

value of Heading in database value of NavigationHeading in database value of Heading in API-response value of NavigationHeading in API-response
<empty> <empty> <empty> <empty>
Heading <empty> Heading <empty>
<empty> NavigationHeading <empty> NavigationHeading
Heading NavigationHeading Heading NavigationHeading

However, we expect the behaviour to be like this (notice the difference in the second row)

value of Heading in database value of NavigationHeading in database value of Heading in API-response value of NavigationHeading in API-response
<empty> <empty> <empty> <empty>
Heading <empty> Heading Heading
<empty> NavigationHeading <empty> NavigationHeading
Heading NavigationHeading Heading NavigationHeading

Why isn't logic expressed in a page type's property "get"-body run when serializing objects through the Content Delivery API? What can we do to achieve this?

- Thomas

#206592
Aug 26, 2019 11:02
Vote:
 

I have the same problem, did you get any answer somewhere else?

#218719
Edited, Mar 19, 2020 7:51
Thomas Wolff - Mar 19, 2020 8:14
Hi Fredric,

No, we didn't get an answer for this question. However, it would be possible to achieve this by customizing the API response as described in the documentation here: https://world.episerver.com/documentation/developer-guides/content-delivery-api/how-to-customize-data-returned-to-clients/

Of course, it would be preferable to keep the logic in the content type definition. However, I have not figured out another way of doing it.

Regards, Thomas
Vote:
 

Yes that's the way I was gonna go since this particular property is only gonna be used by the api but this is problem is gonna grow when more and more people extend their site with contentdelivery api.

#218728
Mar 19, 2020 8:21
Thomas Wolff - Mar 19, 2020 8:22
I totally agree with you. This should absolutely be supported by the content delivery api.
Vote:
 

@Thomas, Time to submit a feature request?

#218741
Mar 19, 2020 15:28
Thomas Wolff - Mar 20, 2020 7:26
@Bob - Yes, great tip! Here it is: https://world.episerver.com/forum/developer-forum/Feature-requests/Thread-Container/2020/3/content-delivery-api-support-logic-expressed-in-property-getters/
Bob Bolt - Mar 20, 2020 13:26
Thank you, Thomas.
* 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.