Copied to clipboard

Flag this post as spam?

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


  • Phill 115 posts 289 karma points
    Sep 09, 2014 @ 06:09
    Phill
    0

    Help with retrieving categorized content

    Hi there,

    I'm having a brain fart and am wondering if someone can help me with what is probably a more general mvc question than an umbraco specific one. I'm created a relatively simple site that will display product listings. I've got a home node and under that various content pages on the site, some pages which use a "product listing" template. This template has a node id that tells it where all products are stored (a separate node at root of site) and a node id that defines it's category (there is also a separate node at root of site will all categories). Products are a document type that can have a single category assigned to them. A category can be a top level or nested sub-category (I currently don't limit levels of categories). So it looks something like:

    Home
    - About
    - Products of Category A
    - Producst of Category B
    - Contact

    Products
    - Product Folder
      - Product 1
      - Product 2

    Categories
    - Category A
      - Category A-1
      - Category A-2
    - Category B

    So if a user lands on a ProductList Page where the default category is set to "Category A" I want to retrieve a list of all products that have been assigned Category A, Category A-1 and Category A-2. Right now I've got a foreach as follows and it works great except it's missing the category/sub-category part and just returns all products. 

    foreach (var group in Umbraco.TypedContent(RootProductNodeID) .Descendants("Product") .Where(x => x.GetPropertyValue("isActive") == true) .InGroupsOf(2))

    So I'm not sure if this is complicated or simple, just can't seem to wrap my head around it without doing a much more elaborate loop with recursive calls and if checks. Can it be done in the where clause?

    Many thanks in advance for any help or pointers.

    Phill

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Sep 09, 2014 @ 10:19
    Ismail Mayat
    0

    Phil,

    I am assuming that the categories are set on the products using multi node tree picker? Whenever I have had to do something like this I use Examine instead of nested loops and the Umbraco api as it gets messy and is not as performant on a large site.  So you could do query to say get me all node types that are product that have category A (if using multi tree node picker it will be the id of category a) Examine uses Lucene.net under the hood and its lightening quick.

    Regards

    Ismail

  • Phill 115 posts 289 karma points
    Sep 09, 2014 @ 20:10
    Phill
    100

    Ok, brain fart it was... gas has passed :)

    I was able to resolve issue by getting a list of categories starting with the toplevel/parent category in question. Then to my where clause I added the following:

    && categoryTree.Any(i => i.Id == x.GetPropertyValue<int>("category"))

    Where categoryTree is IEnumerable<IPublishedContent> list of category nodes. I'm not sure if this is most efficient way but it's working now and definitely seems better than doing any kind of loop over the categories for each item.

    Regards,

    Phill

Please Sign in or register to post replies

Write your reply to:

Draft