hey all, I just upgraded my umbraco 13 project to 17 rc2, and I found that UmbracoContext.Content in RenderController no longer has GetContentType or GetByContentType, if we don’t use examine search how can we query content by document type?
You can inject IPublishedContentQuery from (Umbraco.Cms.Core) and do something like:
var posts = _publishedContentQuery
.ContentAtRoot()
.SelectMany(x => x.DescendantsOrSelf())
.Where(x => x.ContentType.Alias == "blogPost");
Depends a bit on what your site structure is of course, but something like that should work.
If you have modelsbuilder creating strongly typed models for you:
var posts = _publishedContentQuery
.ContentAtRoot()
.SelectMany(x => x.Descendants<BlogPost>());
If you have multiple root nodes you’ll need to find a way to differentiate which one you want to use though and do a .ContentAtRoot().FirstOrDefault(x => x. // your criteria).
If you’re doing this from a View then:
var current = Model;
var root = current.Root();
Will get you to the root node of the current page and then you can do the same query as before on that:
var posts = root
.DescendantsOrSelf()
.Where(x => x.ContentType.Alias == "blogPost");
Or the strongly typed version.
1 Like
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.