Copied to clipboard

Flag this post as spam?

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


  • Myke Bates 10 posts 52 karma points
    Oct 09, 2015 @ 04:35
    Myke Bates
    0

    Querying content by document type outside of a view context

    I have been feverishly trying to find the most succinct and performant ways of querying IPublishedContent like I am normally used to inside of a view context and in specific, being able to leverage collections and filters(love me some DescendantsOrSelf).

    This is a nut I have been trying to crack for a while now and I fear my thinking of how to approach this is either flawed or it just cannot reasonably be done.

    Given the following example - I have hundreds of nodes with the document type alias of "product" which may be nested down in a couple different areas of the site's content tree.

    An example of what I would like to be able to do - In an API Controller, query for all nodes of this type and get a collection of IPublishedContent back to then do whatever with. It is the retrieving that I am stumped on.

    Is there a concept relatively as simple as the following view context Model approach that can be used in an API Controller?

    Model.Content.DescendantsOrSelf().Where(x => x.DocumentTypeAlias == "product");
    

    Or is my thinking flawed?

    Thanks a bunch!

  • Tom Fulton 2030 posts 4989 karma points c-trib
    Oct 09, 2015 @ 06:20
    Tom Fulton
    100

    Hey Mike,

    From an ApiController you can access the UmbracoHelper easily, but of course, it doesn't have the exact method you're after. You can though, do something like this:

    var products = Umbraco.TypedContentAtRoot()
        .First(n => n.IsDocumentType("Homepage"))
        .Children()
        .First(n => n.IsDocumentType("ProductArea"))
        .DescendantsOrSelf("Product");
    

    Hope that helps, Tom

  • Myke Bates 10 posts 52 karma points
    Oct 09, 2015 @ 06:53
    Myke Bates
    0

    YES! I was actually doing something pretty similar to this, but when I tried some of the specifics from your tip, it led me to realizing I did not have access to IsDocumentType and like I have already experienced no access to DescendantsOrSelf. Major derp moment...

    using Umbraco.Web;
    

    face palm

    Might help to to reference that :)

    Thanks for the input as always, Tom!

Please Sign in or register to post replies

Write your reply to:

Draft