Best practice for querying linked items when field allows both reusable and page content types

We're working with Kentico Xperience by Kentico on .NET 9 and need guidance on handling linked content fields that can reference both reusable content types and web page types.

Our Scenario:

We have a PodcastContent reusable content type with GuestInfo and Hosts fields. In the Content Type configuration, these fields allow linking to:

Person - A reusable content type (IContentItemFieldsSource)

PersonPage - A web page type (IWebPageFieldsSource) that contains a linked Person via its PersonContentItem property

The Problem:

Since both types can be linked, the generated PodcastContent property type is:

IEnumerable<IContentItemFieldsSource>

Our current code fails unless we explicitly change the generated file to:

IEnumerable<Person>


Our Questions:

In the example I provided, I also have a HeroImage property that is also a Pages and reusable content type that Allowed Content Types is set to HeroImage and the generated code explicitly changed it to IEnumerable<HeroImage> but left the others generic (I assume purposefully)--do we need to account for each by casting the IContentItemFieldsSource or IWebPageFieldsSource to the correct object type first in code?

What is the recommended pattern for handling linked item fields that allow both reusable content types (IContentItemFieldsSource) and web page types (IWebPageFieldsSource)?

When using WithLinkedItems(depth), does Kentico automatically resolve the nested Person ContentItem on PersonPage, or do we need additional configuration? It wouldn't work unless I changed the type to IEnumerable<Person>

Should we restructure our content model to avoid mixing IContentItemFieldsSource and IWebPageFieldsSource in the same linked items field, or is this a supported scenario?

Thank you for your guidance on the recommended approach.

Environment

Tags:
Content management Content modeling
0

Answers

Accepted answer

Take a look at my answer to another content type architecture question - these kinds of complex models are supported and should work just fine.

  • You are using v30.8.1, correct? That has the Content Retriever API - make sure you use it.
  • We occasionally have instructions in the Changelog to re-generate the C# content type classes to take advantage of new content modeling and querying features. Make sure your content type classes have been generated using your current version of Xperience.
  • The IWebPageFieldsSource and IContentItemFieldsSource types should be the types used in the content type property when there are multiple types that could populate the field. When retrieved by the IContentRetriever using .WithLinkedItems(x), all the linked items will be correctly populated with objects of each C# content type class. You will need to use pattern matching to determine which type of object it is. See an example using switch statements or switch expressions.
0

Sean beat me to it! But the returned object, although the type will be the generic interface class, it IS actually the Person or PersonPage, and you can do a simple:

foreach(var personOrPage in podcast.guestInfo){
  if(personOrPage is Person person) {
    // It's a person, do stuff with person
  } else if(personOrPage is PersonPage personPage) {
    // it's a person page, do stuff with personPage
  }
}
0

Thanks, both of you--Trevor I had to pick Sean's just because it was first! I figured this was the way but for some reason I couldn't get it to work and thought it would be valuable for others regardless. I appreciate the responses!

0

To response this discussion, you have to login first.