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?
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.
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.
@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/
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).
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.
However, we expect the behaviour to be like this (notice the difference in the second row)
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