Copied to clipboard

Flag this post as spam?

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


  • Matt Taylor 785 posts 1802 karma points
    May 24, 2013 @ 11:18
    Matt Taylor
    0

    How to get all nodes that inherit a particular base doc type?

    Hello,

    I'm familiar with ContentHelper.GetChildren<T> which will give me a list of nodes that are of a particular doc type but I have the following doc type structure:

    Planet
    -- Mercury
    -- Venus
    -- Earth
    -- Mars

    So all my various planets inhert from the base 'Planet' doc type.

    These planet nodes will be scattered about in my content so I won't know which particluar type I'm looking for when I'm on a particular node, I just know there will be one:

    Content Root
    -- Node 1
    ---- Mars
    -- Node 2
    ---- Earth
    -- Node 3
    ---- Earth

    Is there a way I can neatly query the child nodes to get any of the planets with out specifying their particular doc type?

    Something like ContentHelper.GetChildren<DocumentTypes.Planet> but it returns anything that inherits from Planet.

    Regards,

    Matt

  • Peter Duncanson 396 posts 1255 karma points c-trib
    May 24, 2013 @ 12:36
    Peter Duncanson
    1

    Hi Matt,

    Yes that possible, we do the same thing. We tend to use interfaces though, for instance we a ISocialMediaItem base class which various other doctypes can extend (or a base doc type if you want to save some work) for twitter, facebook, etc. yet we can render all of them out by passing a list of ISocialMediaItem to our templates. 

    Works a treat.

    Pete

  • Matt Taylor 785 posts 1802 karma points
    May 24, 2013 @ 13:02
    Matt Taylor
    0

    Thanks Peter,

    How do you use ContentHelper to get the list in the first place?

    Regards,

    Matt

  • Peter Duncanson 396 posts 1255 karma points c-trib
    May 24, 2013 @ 13:50
    Peter Duncanson
    100

    You "should" be able to do a little something funky with the DocumentTypeBase returned by GetChildren:

     

        var children = ContentHelper.GetChildren(CurrentModel.Id,true).Where( x => x is Planet);
    

    That should give you a list of just Planet doctypes or anything that inherits from Planet. (http://msdn.microsoft.com/en-us/library/scekt9xw(v=vs.71).aspx)

     

  • Matt Taylor 785 posts 1802 karma points
    May 24, 2013 @ 16:34
    Matt Taylor
    1

    Great Peter, you're the man of the week.

  • Sasa Popovic 77 posts 362 karma points
    May 29, 2013 @ 15:16
    Sasa Popovic
    0

    Hey Matt,

    Don't forget to mark answers as "Solved" when people answer correctly. That helps people who are trying to help to see which questions still need answering but also helps other people who are searching for answers to easilly identify the right answer.

    In this case I believe Pete's answer should be marked as "Solved" and you're the only one who can do it because you opened this thread.

    Regards,
    Sasa

  • Matt Taylor 785 posts 1802 karma points
    May 29, 2013 @ 15:23
    Matt Taylor
    0

    Ah yes, I normally do that. Thanks.

Please Sign in or register to post replies

Write your reply to:

Draft