Copied to clipboard

Flag this post as spam?

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


  • Doogie Talons 183 posts 318 karma points
    Jan 29, 2020 @ 14:50
    Doogie Talons
    0

    Querying the Contents of Multinodetreepicker2

    I am starting this question again as I seem to have made the last thread tottaly unreadable.

    I am trying to query a multinodetreepicker2 element from a page.

    I am trying to get all products that contain a given category id from a multinodetreepicker2 object within the product.

    I have tried two approaches to no effect, well an effect but normally a yellow screen.

    I am hard coding in a categoryNode for testing.

    One option was...

    productSelection = Model.Content.Site()
                                     .FirstChild("primaryBoutique")
                                     .FirstChild("boutiqueProducts")
                                     .Children("boutiqueProduct")
                     .Where(x => x.IsVisible() && x.GetPropertyValue<IEnumerable<string>>("categoryList").Contains("1127"));
    

    categoryList is a multinodetreepicker2 which in this case definatley contains the 1127 node picked. I get the error message.

    [ArgumentNullException: Value cannot be null. Parameter name: source]

    There is only one product published and it has the 1127 picked.

    The other option which kind of works... although not really is this.

    productSelection = startNode
                                     .Children("boutiqueProduct")
                    .Where("visible")
                    .Where("@categoryList.ToString().Contains(@0)", "1127");
    

    This fires but returns no results, so I removed the filter and checked the properties of categoryList it returned.

    System.Collections.Generic.List`1[Umbraco.Core.Models.IPublishedContent]

    So on a whim I changed the code to

    productSelection = startNode
                                         .Children("boutiqueProduct")
                        .Where("visible")
                        .Where("@categoryList.ToString().Contains(@0)", "System");
    

    Which returned the published product, leading me to believe the query is not querying the contents of categoryList more its value as a string, so is there a way to rewrite either query to read the categories selected inside categoryList rather than it's actual object type.

    Regards

    Doogie

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jan 29, 2020 @ 15:30
    Alex Skrypnyk
    1

    Hi

    Try this code:

    var productSelection = startNode
            .Children().Where(x => x.DocumentTypeAlias.Equals("boutiqueProduct") && x.GetPropertyValue<bool>("visible") && x.GetPropertyValue<string>("categoryList").Contains("1127"));
    
  • Doogie Talons 183 posts 318 karma points
    Jan 29, 2020 @ 15:57
    Doogie Talons
    0

    If I put your line in the first example.

    var productSelection = Model.Content.Site()
                           .FirstChild("primaryBoutique")
                           .FirstChild("boutiqueProducts")
                           .Children()
                           .Where(x => x.DocumentTypeAlias.Equals("boutiqueProduct") && x.GetPropertyValue<bool>("visible") && x.GetPropertyValue<string>("categoryList").Contains("1127"));
    

    It does not crash or give me the null error but returns nothing.

    If I put it in the second option.
    var startNode = Umbraco.Content(1169);
    var productSelection = startNode
                           .Children("boutiqueProduct")
                           .Where("visible")
                           .Children()
                           .Where(x => x.DocumentTypeAlias.Equals("boutiqueProduct") && x.GetPropertyValue<bool>("visible") && x.GetPropertyValue<string>("categoryList").Contains("1127"));
    

    I get the error

    Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jan 29, 2020 @ 16:01
    Alex Skrypnyk
    0

    Use strongly typed objects:

    var startNode = Umbraco.TypedContent(1169);
    
  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jan 29, 2020 @ 16:02
    Alex Skrypnyk
    0
        var startNode = Umbraco.TypedContent(1169);
        var productSelection = startNode.Children
            .Where(x => x.DocumentTypeAlias.Equals("boutiqueProduct") && x.GetPropertyValue<string>("categoryList").Contains("1127"));
    
  • Doogie Talons 183 posts 318 karma points
    Jan 29, 2020 @ 16:22
    Doogie Talons
    0

    Ok if I do...

    var startNode = Umbraco.TypedContent(1169);
    var productSelection = startNode.Children()
                           .Where(x => x.GetPropertyValue<bool>("visible") && x.DocumentTypeAlias.Equals("boutiqueProduct"));
    

    Nothing returns at all but it doesn't crash.

    If I do...

    var startNode = Umbraco.TypedContent(1169);
    var productSelection = startNode.Children()
                           .Where(x => x.IsVisible() && x.DocumentTypeAlias.Equals("boutiqueProduct"));
    

    It returns all published products, as expected.

    If I do

    var startNode = Umbraco.TypedContent(1169);
    var productSelection = startNode.Children()
                           .Where(x => x.IsVisible() && x.DocumentTypeAlias.Equals("boutiqueProduct") && x.GetPropertyValue<string>("categoryList").Contains("1127"));
    

    It doesn't crash but returns nothing.

    If I do...

    var startNode = Umbraco.TypedContent(1169);
    var productSelection = startNode.Children()
                           .Where(x => x.IsVisible() && x.DocumentTypeAlias.Equals("boutiqueProduct") && x.GetPropertyValue<string>("categoryList").Contains("System"));    
    

    It returns all products.

    If I render...

    @productV.GetPropertyValue("categoryList").ToString()
    

    after calling the products it returns...

    System.Collections.Generic.List`1[Umbraco.Core.Models.IPublishedContent]

    Which makes me think that the object type is being queried rather than it's contents.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jan 29, 2020 @ 16:32
    Alex Skrypnyk
    101

    Try this

    var startNode = Umbraco.TypedContent(1169);
        var productSelection = startNode.Children()
            .Where(x => x.IsVisible() && x.DocumentTypeAlias.Equals("boutiqueProduct") && x.GetPropertyValue<IEnumerable<IPublishedContent>>("categoryList").FirstOrDefault(category => category.Id == 1127) != null); 
    
  • Doogie Talons 183 posts 318 karma points
    Jan 29, 2020 @ 16:48
    Doogie Talons
    0

    Cool that works.

    So at the moment I am hard coding in the categoryId I will want to pass that as a varaible from the id of the page I am on, I guess I will have to convert it to an int along the way ?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jan 29, 2020 @ 17:01
    Alex Skrypnyk
    0

    yes, it's int already - just replace it with real id

  • Doogie Talons 183 posts 318 karma points
    Jan 29, 2020 @ 17:09
    Doogie Talons
    0

    Cool I am passing it as a query to an ajax function so it arrives as a string. I can parse the string to an int then call it like..

    int categoryId;
    if(int.TryParse(Request["catId"], out categoryId))
    {
    
    }
    else
    {
       categoryId  = 0;
    }
    
    var productSelection = startNode.Children
                           .Where(x => x.IsVisible() && x.DocumentTypeAlias.Equals("boutiqueProduct") && x.GetPropertyValue<IEnumerable<IPublishedContent>>("categoryList").FirstOrDefault(category => category.Id == categoryId) != null)
    

    I can't seem to mark your reply as solution :)

    And thank you very much for your time.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jan 29, 2020 @ 17:14
    Alex Skrypnyk
    0

    You are always welcome, happy to help.

    Did you mention that category is a current page in Umbraco? If so, you can use currentPage like that:

    var productSelection = startNode.Children
        .Where(x => x.IsVisible() && x.DocumentTypeAlias.Equals("boutiqueProduct") && x.GetPropertyValue<IEnumerable<IPublishedContent>>("categoryList").FirstOrDefault(category => category.Id == Umbraco.AssignedContentItem.Id) != null)
    
  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jan 29, 2020 @ 17:14
    Alex Skrypnyk
    0

    Umbraco.AssignedContentItem is an easiest way to get the current node.

  • Doogie Talons 183 posts 318 karma points
    Jan 29, 2020 @ 17:16
    Doogie Talons
    1

    Yeah it's the current page, but I pass the ID to an ajax function on another page to render it's contents so I send as query string along with the number of items to render, then a show more button to load more etc.

    Thanks again.

Please Sign in or register to post replies

Write your reply to:

Draft