Copied to clipboard

Flag this post as spam?

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


  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Feb 04, 2018 @ 13:51
    Michaël Vanbrabandt
    0

    GetPropertyValue issues using Nested content and MNTP

    Hi all,

    I am facing some strange issues with Nested Content and GetPropertyValue.

    In other projects ( < v7.6.x ) I was using the following code to get the Id of the IPublishedContent assigned to a property on the NC Document Type:

        public static bool CheckCategory(this IPublishedContent node, int category)
        {
            // Nested content property which contains a list of the assigned packages
            var packages = node.GetPropertyValue<IEnumerable<IPublishedContent>>("packages");
    
            return packages.Select(p => p.GetPropertyValue<int>("category")).Contains(category);
        }
    

    But this doesn't work anymore in a project running 7.7.9.

    I had to change the code to the following in order to make this work. But I don't like it and prefer to old way which was excellent.

        public static bool CheckCategory(this IPublishedContent node, int category)
        {
            // Nested content property which contains a list of the assigned packages
            var packages = node.GetPropertyValue<IEnumerable<IPublishedContent>>("packages");
            var result = packages.Select(p => p.GetPropertyValue<IEnumerable<IPublishedContent>>("category").First().Id);
            return result.Contains(category);
        }
    

    The property Category is a Multinode Treepicker which is setup to only have one node.

    Any ideas?

    /Michaël

  • Kevin Jump 2341 posts 14868 karma points MVP 8x c-trib
    Feb 04, 2018 @ 17:39
    Kevin Jump
    100

    Hi

    in Umbraco 7.6 Property Value Converters are on by default, which is why your MNTP is returning IEnumberable

    but equally the default MNTP (MultiNodeTreePicker2) in Umbraco 7.6 stores UID values not Int values - so if you are using the new pickers you won't be able to get to the int values.

    If you want to still use the value converters (or if you using the new pickers) you can simplify the query a bit.

    var packages = Model.GetPropertyValue<IEnumerable<IPublishedContent>>("packages");
    var contains = packages
        .Any(x => x.GetPropertyValue<IEnumerable<IPublishedContent>>("category").Any(y => y.Id == category));
    

    there is probably some clever xpath that does this really fast too - but the above will do it.

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Feb 05, 2018 @ 08:18
    Michaël Vanbrabandt
    0

    Hi Kevin,

    was already thinking of something in that direction that had to be changed in newer version.

    After some debugging I did find the UID that was stored but didn't know what it was untill now.

    So, something new learned today thanks!

    /Michaël

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Feb 05, 2018 @ 08:40
    Michaël Vanbrabandt
    0

    Any xpath pro that can give the xpath version for doing the same thing?

    /Michaël

  • Damiaan 442 posts 1302 karma points MVP 6x c-trib
    Feb 05, 2018 @ 11:22
    Damiaan
    0

    Xpath to loop the NestedContent? I don't know if you could make that work.

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Feb 05, 2018 @ 12:12
    Michaël Vanbrabandt
    0

    Damiaan,

    ok then I will hold on the query way!

    Thnx!

    /Michaël

Please Sign in or register to post replies

Write your reply to:

Draft