Copied to clipboard

Flag this post as spam?

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


  • Dipa 88 posts 370 karma points
    Dec 14, 2015 @ 07:16
    Dipa
    0

    Get subchild of multiple node using single query

    Hello,

    I Want to display product category and its product. I have displayed it.

    My node structure is like :

    Home
        Product Category-1
            Product Category List-1.1
                Product-1.1.1
                Product-1.1.2
            Product Category List-1.2
                Product-1.2.1
                Product-1.2.2
        Product Category-2
            Product Category List-2.1
                Product-2.1.1
                Product-2.1.2
            Product Category List-2.2
                Product-2.2.1
                Product-2.2.2
    

    Now, I want to get
    Product-1.1.1, Product-1.1.2, Product-1.2.1, Product-1.2.2, Product-2.1.1, Product-2.1.2, Product-2.2.1, Product-2.2.2 in single query. How can I get that?

    Thank You.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Dec 14, 2015 @ 07:36
    Dave Woestenborghs
    100

    Hi Dipa,

    You can do something like this :

    @using Umbraco.Core.Models
    @using Umbraco.Web
    @using Umbraco.Web.Mvc
    @inherits UmbracoViewPage
    @{
        var homeNode = Model.AncestorOrSelf("DocTypeAliasOfHomeNode");
    
        List<IPublishedContent> categories = new List<IPublishedContent>();
    
        if(homeNode != null)
        {
            categories = homeNode.Descendants("DocTypeOfCategory").ToList();
        }
    }
    
    @if(categories.Any())
    {
        <ul>
            @foreach(var category in categories)    
            {
                <li><a href="@category.Url">@category.Name</a></li>    
            }
        </ul>
    }
    

    Of course you need to replace the doctype aliases with the correct ones. Also when having a lot of categories the .Descendants call can be a performance hit. So make sure that you cache this information.

    Dave

  • Dipa 88 posts 370 karma points
    Dec 14, 2015 @ 09:55
    Dipa
    0

    Hi Dave,

    Its worked for me. Thanks.

  • Dipa 88 posts 370 karma points
    Dec 14, 2015 @ 10:29
    Dipa
    0

    Hi Dave,

    Your help is really helpful to me. But now, in project, node structure get changed. Now it is like below :

    Home
            Page-1
            Page-2
    Product Category-1 
            Product Category List-1.1
                Product-1.1.1
                Product-1.1.2
            Product Category List-1.2
                Product-1.2.1
                Product-1.2.2 
    Product Category-2
            Product Category List-2.1
                Product-2.1.1
                Product-2.1.2
            Product Category List-2.2
                Product-2.2.1
                Product-2.2.2
    

    Now how can I get only products?

    Thanks,

    Dipa

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Dec 14, 2015 @ 12:13
    Dave Woestenborghs
    0

    You can actually you use the same code.

    Dave

  • Dipa 88 posts 370 karma points
    Dec 14, 2015 @ 12:20
    Dipa
    0

    Thanks Dave for replying soon.

    But my doubt is :

    Document Type of Home node and Product category node is different. Then which document type I have to define as DocTypeAliasOfHomeNode ? Of home node or Product Category node ?

    Thanks,

    Dipa

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Dec 14, 2015 @ 12:31
    Dave Woestenborghs
    0

    Hi Dipa,

    Maybe i should explain the code a bit.

     var homeNode = Model.AncestorOrSelf("DocTypeAliasOfHomeNode");
    

    This code will traverse up the tree from your current page and will find the ancestor of the type homepage. If it is executed on the homepage it will return the homepage as well. You should replace "DocTypeAliasOfHomeNode" with the actual doctype alias of your home node.

    Then this piece of code will get all your pages under the "Home" node of the specified document type, no matter on what level they are in the tree.

    List<IPublishedContent> categories = new List<IPublishedContent>();
    
        if(homeNode != null)
        {
            categories = homeNode.Descendants("DocTypeOfCategory").ToList();
        }
    

    You should replace "DocTypeOfCategory" to the actual doctype of your product.

    Dave

  • Dipa 88 posts 370 karma points
    Dec 14, 2015 @ 12:56
    Dipa
    0

    Thanks Dave.

    I got it.

  • Dipa 88 posts 370 karma points
    Dec 15, 2015 @ 05:53
    Dipa
    0

    Hi Dave,

    Again I got one issue. If I run the page of Product category, I am getting exact output that I want. But now I want that When I run the page of Home node, I want product list.

    When line

     var homeNode = Model.AncestorOrSelf("DocTypeAliasOfHomeNode");
    

    get executed it is giving null. Not getting how to get it. Help me.

    Thanks,

    Dipa

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Dec 15, 2015 @ 07:53
    Dave Woestenborghs
    0

    Did you replace "DocTypeAliasOfHomeNode" with the doctype alias of your home documenttype ?

    Or maybe you can try this instead :

    var homeNode = Model.AncestorOrSelf("DocTypeAliasOfHomeNode");
    

    Dave

  • Dipa 88 posts 370 karma points
    Dec 15, 2015 @ 08:13
    Dipa
    0

    Hi Dave

    I replaced it but not working.

    Dipa

  • Dipa 88 posts 370 karma points
    Dec 15, 2015 @ 08:26
    Dipa
    0

    Hi Dave

    I want product list in Home page. For that my project structure is like below. Just go through it.

    Home
            Page-1
            Page-2
    Product Category-1 
            Product Category List-1.1
                Product-1.1.1
                Product-1.1.2
            Product Category List-1.2
                Product-1.2.1
                Product-1.2.2 
    Product Category-2
            Product Category List-2.1
                Product-2.1.1
                Product-2.1.2
            Product Category List-2.2
                Product-2.2.1
                Product-2.2.2
    

    I want product in home page.

    Thanks

    Dipa

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Dec 15, 2015 @ 09:37
    Dave Woestenborghs
    0

    So your product categories are on the same level as the homepage ?

    Dave

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Dec 15, 2015 @ 09:45
    Dave Woestenborghs
    0

    You can probably change your code to this :

    var homeNode = Umbraco.TypedContentAtRoot().First();
    

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft