Does Umbraco 18 offer a service or API to retrieve reusable elements from the Library by Element Type?
For example, I have a “Frequently Asked Questions” Element Type enabled for use in the Library. How can I retrieve elements of this type in C# without knowing each element’s GUID?
Also, is there an equivalent to ContentAtRoot() or MediaAtRoot() for Library elements, or a recommended service for querying them by Element Type alias?
I found this IElementContainerService, which had an AsEnumerableOfOne method, but it is now deprecated.
I have found another one: IElementNavigationQueryService. Are there other services besides these two?
Hi @sandhuravinder
IElementNavigationQueryService is the one you want, though it doesn’t have ContentAtRoot(). It’s all key based, so you get TryGetRootKeys and TryGetRootKeysOfType(alias, out keys) for the roots, then TryGetChildrenKeys and TryGetDescendantsKeysOfType to walk down from there. You then pass those keys through IPublishedElementCache to get the actual IPublishedElement back.
If you only want everything of one type and don’t care about where it sits, IElementCacheService.GetByContentType does it in one call. Bear in mind that’s a database hit and it hands you unpublished items as drafts, so I’d cache it and filter.
(Researched with the help of AI and fact checked against the Umbraco course code).
Justin