Copied to clipboard

Flag this post as spam?

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


  • Shelly Coops 7 posts 88 karma points
    Jun 07, 2019 @ 12:39
    Shelly Coops
    0

    Getting all content using documenttype

    I'm having trouble retrieving all contents based on the document type.

    I have the following hierarchy in my Content section:

    • Home
      • Destination
        • Europe
        • America
      • Tours
        • Bus
        • Boats
        • Road

    I need to retrieve only the content under the node Tours and all three of the have the same Document Type " tourType "

    I used to do it like this on Umbraco 7:

    var tours = Model.Content.Children(x=>x.DocumentTypeAlias=="tourType").Where("Visible");
    

    How should it be done now in 8?

  • Jonathan Distenfeld 84 posts 459 karma points
    Jun 07, 2019 @ 12:55
    Jonathan Distenfeld
    1

    Hello Shelly,

    in Umbraco 8 you should be able to achieve this using following:

    Strongly typed:

    var tours = Model.Children<TourType>().Where(x => x.IsVisible());
    

    Not strongly typed:

    var tours = Model.Children(x => x.IsDocumentType("tourType") && x.IsVisible());
    

    Hope I could help you.

    Regards, Jonathan

  • Shelly Coops 7 posts 88 karma points
    Jun 10, 2019 @ 05:22
    Shelly Coops
    0

    Hi Jonathan,

    I've tried your solution.

    However, it's still not working for me.

    I get a compilation error:

    Compiler Error Message: CS0246: The type or namespace name 'TourType' could not be found (are you missing a using directive or an assembly reference?)

    I've changed this value with the document type id and even tried others and I'm still not getting any results.

    What am I doing wring here?

  • Marc Goodson 995 posts 6568 karma points MVP 4x c-trib
    Jun 10, 2019 @ 08:13
    Marc Goodson
    0

    Hi Shelly

    In addition to the suggestions above there is a 'ChildrenOfType' extension on IPublishedContent that you could also use:

    var tours =  Model.ChildrenOfType("tourType").Where(f=>f.IsVisible());
    

    regards

    Marc

  • Emil Lindgren 22 posts 183 karma points
    Jun 10, 2019 @ 08:46
    Emil Lindgren
    0

    It might work with:

    var content = Model.Content.Site().FirstChild("tours");
    

    Then you can access children etc.

    /Emil

  • Pantelis 51 posts 105 karma points
    Aug 07, 2019 @ 19:24
    Pantelis
    0

    Testing the ChildrenOfType extension in 8.1.0 doesn't work, it just returns the same result with Children, that is all children. I guess this is a bug.

    My only success was with:

    var categories = parentNode.Children(c => c.IsDocumentType("category"));
    

    or

    var categories = parentNode.Children(c => c.ContentType.Alias == "category");
    
Please Sign in or register to post replies

Write your reply to:

Draft