Umbraco 12 - get content by documentTypeimprovisation
r
Solution is a 2 language application and has two root nodes "Home" and "Data". Umbraco.ContentAtRoot().DescendantsOrSelfOfType(doc1.ModelTypeAlias).FirstOrDefault()?.Children().OfType() My understanding on how the DescendantsOrSelf works is Umbraco fetch the first node and traverse all its children and then comes to next node and traverse through all its children and keeping searching until it finds a node that matches the "doc1.ModelTypeAlias". This doc1 content is the last content in the second root node. doc1 contains 20000 children. Total nodes excluding doc2 is 2500 apprx. What is the best approach to get a content by documentType?
r
Any
Descendants
query is going to be expensive to run (see https://docs.umbraco.com/umbraco-cms/reference/common-pitfalls#querying-with-descendants-descendantsorself) so I would consider rewriting it something like this... (may not be 100% correct!)
Copy code
csharp
// Get 'Home'
var rootNode = Umbraco.ContentAtRoot().FirstOrDefault(x => x.IsDocumentType(Home.ModelTypeAlias);

// Get first 'Doc1' type
var doc1Node = rootNode?.FirstOrDefault(x => x.IsDocumentType(doc1.ModelTypeAlias);

// Get all 'Doc2' children
var doc2Node = doc1Node?.Children<doc2>();
That removes the descendant query which can traverse the whole tree and only searches the next level down each time
k
What is the recommended "global GetContentByDocType" approach? Descendants? Examine? Custom index? I know there are discussions on GitHub from when the v8 stuff was removed but I'm guessing some best-practice has evolved by now.
m
If I recall @CodeSharePaul did a talk on this for v9 and its all about how much content you have... Linq queries are faster up until a point then examine takes over. GetContentByDocType - if thats on cache will be similar to any other linq query, if its on the Content service its a database query and always slow
r
which one of the below is query on cached content ? a. IPublishedContentQuery b. UmbracoHelper c. IUmbracoContextAccessor d. IUmbracoContextFactory @User
m
All of them will when you get to querying
Where and how you use them all depends on what your upto
30 Views