Copied to clipboard

Flag this post as spam?

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


  • Phil 54 posts 139 karma points
    Oct 22, 2014 @ 12:38
    Phil
    0

    Get list of Descendants with document type and property value

    I am trying to get a list of Descendants from the root node with a certain type and property value. This is from a PartialViewMacro page.

    On document type myType, there is a textstring property myProperty. To extract all nodes with the relevant document type, and a value of "blue" stored in myProperty, I am using the following code:

    //get all nodes with correct document type
    var list = CurrentPage.AncestorOrSelf(1).Descendants("myType");
    
    //filter on myProperty
    var filteredList = list.Where("myProperty == @0", "blue");
    

    However, filteredList is always empty, even though list contains multiple nodes with the correct document type and a property value of "blue".

    Are there any better approaches to achieve the correct results that I could try?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Oct 22, 2014 @ 13:01
    Dennis Aaen
    0

    Hi Phil,

    I have just tried to use the code that you have posted.

    And it work fine for me. I have added a property with the alias of newprop, and in my example it uses the textsting data type.

    Here is how my code looks like, it just pull out the name of the item.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
        @{
            var list = CurrentPage.AncestorOrSelf(1).Descendants("umbNewsItem");
            var filteredList = list.Where("newprop == @0", "blue");
           
        }
        <ul>
            @foreach(var item in filteredList){
                <li>
                    <p>@item.Name</p>
                </li>
            }
        </ul>

    Hope this helps,

    /Dennis

  • Phil 54 posts 139 karma points
    Oct 22, 2014 @ 13:57
    Phil
    0

    @Dennis,

    I have no idea what changes I have made, but upon comparing my .cshtml file to yours, and deleting/re-creating the macro, the code is working. All I can assume is that there was some kind of typo that I originally missed - just shows the importance of checking for the simplest mistakes!

    Your help is very much appreciated.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Oct 22, 2014 @ 14:17
    Dennis Aaen
    0

    Hi Phil,

    You are welcome, and great it works now,

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft