I've got a 'folder' in the content tree that could have many document types in it and many levels and I need to find the all of the content that contained within this folder that is of a certain document type. So basically I want to search this folders entire structure and return all of the specified documents.
And I can't find out how this is achieved in Razor syntax.
I know that I can search the entire content tree but I'm trying to target a specific folder.
Could anyone assist please?
To further complicate things I need to order them by date created (and only return the top 3!).
You can of course traverse the content tree using Children() or Descendants(). However Descendants() should be avoided when possible - especially when having a large content tree with many nodes and levels.
I would be much better to using XPath - you can easily find all nodes below a specific folderdocument type.
var xpath = "//Folder/*";
or from a specific root node:
var xpath = string.Format(@"//Folder[ancestor::Home[@id = ""{0}""]]/*", rootNodeId);
You should then be able to use UmbracoHelper to get the nodes from XPath. Something like this:
var nodes = Umbraco.TypedContentAtXPath(xpath).OrderBy(x => x.GetPropertyValue<DateTime>("createDate")).Take(3);
Thanks for your reply :) I've attempted your solution but sadly I'm getting no results (and there's loads of content under the content node).
I've attached a screenshot of my code and the results in case I've missed something obvious (just to confirm the node 1100 is a document type as oppose to a folder.).
Find content by Document Type under node
Hi folks,
I've got a 'folder' in the content tree that could have many document types in it and many levels and I need to find the all of the content that contained within this folder that is of a certain document type. So basically I want to search this folders entire structure and return all of the specified documents.
And I can't find out how this is achieved in Razor syntax.
I know that I can search the entire content tree but I'm trying to target a specific folder.
Could anyone assist please?
To further complicate things I need to order them by date created (and only return the top 3!).
Thanks in advance.
C
Hi Craig
You can of course traverse the content tree using
Children()
orDescendants()
. HoweverDescendants()
should be avoided when possible - especially when having a large content tree with many nodes and levels.I would be much better to using
XPath
- you can easily find all nodes below a specificfolder
document type.or from a specific root node:
You should then be able to use
UmbracoHelper
to get the nodes from XPath. Something like this:/Bjarne
Hi Bjarne,
Thanks for your reply :) I've attempted your solution but sadly I'm getting no results (and there's loads of content under the content node).
I've attached a screenshot of my code and the results in case I've missed something obvious (just to confirm the node 1100 is a document type as oppose to a folder.).
Thanks again, Craig
Hi Craig,
Folder in the XPath should be the folder node's DocumentTypeAlias (just in case that wasn't obvious from Bjarnes post).
Also - which type (DocumentTypeAlias) do you want to end up selecting?
E.g.. this XPpath should get you any NewsItem that's inside or below any Folder node below the node with
id=1100
:Hope that helps,
/Chriztian
Hi Chriztian,
Thanks for your help sadly I'm still getting no results :(
What I've got is:
var xpath = string.Format("id({0})//uBlogsyLanding//uBlogsyPost", 1100);
The Node ID of 1100 relates to the root node of the uBlogsyLanding page and I'm trying to extract the last three posts that live under this node.
Here's a screenshot of my content tree.
Thanks my friend.
Ah but of course - if the 1100 is the uBlogsyLanding then it makes sense (because you have no uBlogsyLanding below that one).
Dave's suggestion should work fine then - and if it gets slow, there's plenty of ways to optimise it (but XPath is fast :-)
/Chriztian
Hi Craig,
Can you try this :
This is a bit more greedy. I suspect there is not uBlogsyLanding page in your tree.
Dave
A ha, beautiful work gents :)
Thanks for your assistance
Hi again,
Just one more thing I think that this is giving the oldest 3 posts first as opposed to the latest 3. I can't seem to get a desc or reverse to work?
Thanks, C
Hi Craig,
There is a OrderByDescending method. That would do the trick.
If you want really superfast speed you can use a xpath navigator.
You can see a example of what your are looking for here : https://www.slideshare.net/dawoe/the-need-for-speed-uk-fest on slide 55
This uses models builder so you can ditch the
.OfType<NewsDetailpage>
Dave
Thanks Dave,
Strange one this as orderby and orderbydescending both remove the latest posts as opposed to the oldest ones...
var nodes = Umbraco.TypedContentAtXPath(xpath).OrderBy(x => x.GetPropertyValue
var nodes = Umbraco.TypedContentAtXPath(xpath).OrderByDescending (x => x.GetPropertyValue
Both produce the same results......
Hi Craig,
Can you try this :
So the property is actually converted to a date when ordering ?
Dave
Sorry yes,
That produces the same results i.e. OrderBy and OrderByDescending return identical posts.
Hi Craig,
Is createDate a actually property on your doctype ?
Or do you just want to sort on the create date of the content item?
Dave
Hi Dave,
I thought that createDate related to the datestamp of the content item creation date??
Is that not the case?
C
Hi Craig,
Can you try this :
This will sort on the create date of the content item
Dave
Perfect!
I'm such a loser!
Thanks for your time my friend
There are no losers here - we all win :-)
/Chriztian
No problem mate....We all have been there once.
A fresh pair of eyes sometimes can see the obvious.
Dave
is working on a reply...