Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Craig O'Mahony 364 posts 918 karma points
    Jan 11, 2018 @ 22:36
    Craig O'Mahony
    0

    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

  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    Jan 11, 2018 @ 23:00
    Bjarne Fyrstenborg
    2

    Hi Craig

    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);
    

    /Bjarne

  • Craig O'Mahony 364 posts 918 karma points
    Jan 12, 2018 @ 09:25
    Craig O'Mahony
    0

    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.).

    enter image description here

    Thanks again, Craig

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Jan 12, 2018 @ 09:37
    Chriztian Steinmeier
    1

    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:

    var xpath = string.Format("id({0})//Folder//NewsItem", 1100);
    

    Hope that helps,

    /Chriztian

  • Craig O'Mahony 364 posts 918 karma points
    Jan 12, 2018 @ 09:56
    Craig O'Mahony
    0

    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.

    enter image description here

    Thanks my friend.

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Jan 12, 2018 @ 10:28
    Chriztian Steinmeier
    1

    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

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jan 12, 2018 @ 10:23
    Dave Woestenborghs
    102

    Hi Craig,

    Can you try this :

    var xpath = string.Format("id({0})//uBlogsyPost", 1100);
    

    This is a bit more greedy. I suspect there is not uBlogsyLanding page in your tree.

    Dave

  • Craig O'Mahony 364 posts 918 karma points
    Jan 12, 2018 @ 10:30
    Craig O'Mahony
    0

    A ha, beautiful work gents :)

    Thanks for your assistance

  • Craig O'Mahony 364 posts 918 karma points
    Jan 12, 2018 @ 10:41
    Craig O'Mahony
    0

    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

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jan 12, 2018 @ 10:43
    Dave Woestenborghs
    0

    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

  • Craig O'Mahony 364 posts 918 karma points
    Jan 12, 2018 @ 10:59
    Craig O'Mahony
    0

    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......

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jan 12, 2018 @ 11:00
    Dave Woestenborghs
    0

    Hi Craig,

    Can you try this :

    var nodes = Umbraco.TypedContentAtXPath(xpath).OrderByDescending (x => x.GetPropertyValue<DateTime>("createDate")).Take(3);
    

    So the property is actually converted to a date when ordering ?

    Dave

  • Craig O'Mahony 364 posts 918 karma points
    Jan 12, 2018 @ 11:04
    Craig O'Mahony
    0

    Sorry yes,

    That produces the same results i.e. OrderBy and OrderByDescending return identical posts.

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jan 12, 2018 @ 11:32
    Dave Woestenborghs
    0

    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

  • Craig O'Mahony 364 posts 918 karma points
    Jan 12, 2018 @ 11:34
    Craig O'Mahony
    0

    Hi Dave,

    I thought that createDate related to the datestamp of the content item creation date??

    Is that not the case?

    C

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jan 12, 2018 @ 11:36
    Dave Woestenborghs
    0

    Hi Craig,

    Can you try this :

    var nodes = Umbraco.TypedContentAtXPath(xpath).OrderByDescending (x => x.CreateDate).Take(3);
    

    This will sort on the create date of the content item

    Dave

  • Craig O'Mahony 364 posts 918 karma points
    Jan 12, 2018 @ 11:38
    Craig O'Mahony
    0

    Perfect!

    I'm such a loser!

    Thanks for your time my friend

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Jan 12, 2018 @ 11:45
    Chriztian Steinmeier
    1

    There are no losers here - we all win :-)

    /Chriztian

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jan 12, 2018 @ 11:40
    Dave Woestenborghs
    0

    No problem mate....We all have been there once.

    A fresh pair of eyes sometimes can see the obvious.

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft