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

Giuliano Dore
Mar 13, 2021
  2416
(10 votes)

The ultimate set of extensions to navigate EPiServer content trees

As I have been working with CMS for a while, I always found content tree navigation to be one of the most important aspects of development for professional projects.

I have been writing about functions to do content tree navigations using EPiServer core functions here and here and with my team we finally decided to open-source and package some of our most popular functions for database-free content search using IContentLoader with our new (beta) package:

Dotcentric.Extensions

The current library is focused on extending IContentLoader to offer a range of helper functions for IContentData and IContent objects:

  • FirstChild - retrieve the first child of a content item - possibly filtered based on an optional predicate.
  • FirstSibling - retrieve the first sibling of a content item - possibly filtered based on an optional predicate.
  • FollowingSibling - retrieve the following sibling of a content item based on a sorting parameter like the name or sort index, etc.
  • FollowingSiblings - retrieve the following siblings of a content item based on a sorting parameter. Examples are above.
  • GetAncestor - retrieve the first ancestor of a content item matching some criteria.
  • GetAncestorOrSelf - retrieve the first ancestor or the current content item based on some criteria.
  • GetAncestors - retrieve a list of ancestors - possibly filtered by an optional predicate.
  • GetDescendent - retrieve the first descendent of the current content item based on some criteria.
  • GetDescendentOrSelf - retrieve the first descendent or the current content item based on some criteria.
  • GetDescendents - retrieve the list of descendents from the current content item, possibly filtered by an optional predicate.
  • LastChild - retrieve the last child of a content item - possibly filtered based on an optional predicate.
  • OrderedSiblingsAndSelf - retrieve the ordered list of siblings + the current content item based on a sorting parameter.
  • PreviousSibling - retrieve the previous sibling of a content item based on a sorting parameter.
  • PreviousSiblings - retrieve the list of previous siblings of a content item based on a sorting parameter.
  • Siblings - retrieve the list of siblings of a content item
  • SiblingsAndSelf - retrieve the list of siblings of a content item + the item itself.

Some notes about our micro-library:

As we are working with content tree items and concepts like parents, ancestors, children and descendents, most of the functions include a generic type constraint where the content item must inherit from IContent. The reason being that this is the highest abstraction available that contains a ParentLink property, mandatory for us to do bottom up navigation for functions like GetAncestor.

The project is available on github: https://github.com/dotcentric/Dotcentric.EPiServer.Extensions - I added an Alloy website project that I extended to offer a TestsPage where I tried some scenarios like:

  • find all the ancestors of the current page with more than 2 children
  • find all the "standard page" ancestors of the 'Find a reseller' page
  • find the next sibling by name of the page named 'alloy plan'

The "tests" are available here: https://github.com/dotcentric/Dotcentric.EPiServer.Extensions/blob/main/Dotcentric.Extensions/Dotcentric.Extensions.Website/Controllers/TestsPageController.cs - I didn't use unit-tests as it felt like a massive amount of work.

As we used the best abstraction we could find, we can use the helper functions for the CMS content tree but also for the EPiServer Commerce tree and enable the  behaviors like these:

  • I want to retrieve all the products of a specific type.
  • I want to retrieve the siblings of a category based on a specific name
  • I want to retrieve all the catalogs that have more than 2 categories.

If you find a bug or a possible improvement feel free to send a PR, feedback is more than welcome 😁

Cheers,

Giuliano

Mar 13, 2021

Comments

Sam Brooks
Sam Brooks Mar 13, 2021 04:17 PM

This package will be really useful to us. One of the tasks I find myself sinking time into at the early stage of every new episerver project is to add content loader extensions like this as they really help to reduce repetition in the code. Having it all in a package like this will save me a lot of time.

Jon Sexton
Jon Sexton Mar 17, 2021 01:14 PM

Excellent notion of an extension that will undoubtedly save a lot of time and effort moving forward, well done Giuliano.

Please login to comment.
Latest blogs
Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

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