Copied to clipboard

Flag this post as spam?

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


  • Martin Rud 103 posts 295 karma points
    Nov 08, 2020 @ 20:17
    Martin Rud
    0

    Find nodes in which a specific node is referenced in a MNTP

    I have a MNTP property "tags" on different nodes across my site tree. On this property the editors can pick from category nodes. Say I have a category node with id 1151 - then I want to query for all nodes that has this node referenced in their "tags" property.

    I found something I thought was right here (https://our.umbraco.com/forum/using-umbraco-and-getting-started/97468-get-and-store-values-of-a-multi-node-tree-picker-into-a-list-of-strings and https://our.umbraco.com/packages/developer-tools/umbraco-core-property-value-converters/feedback/64461-How-to-query-for-mntp-value) and made it v8 compatible (changed "GetPropertyValue" to "Value"):

    var articles = Umbraco.ContentAtRoot().FirstOrDefault().Descendants()
       .Where(x => x.Value<IEnumerable<IPublishedContent>>("tags").Select(y => y.Id).Contains(1151));
    

    But I get this error (on the second line):

    System.ArgumentNullException: Value cannot be null.
    Parameter name: source
    

    Any ideas?

  • Steve Morgan 1278 posts 4216 karma points c-trib
    Nov 09, 2020 @ 15:25
    Steve Morgan
    100
    var articles = Umbraco.ContentAtRoot().FirstOrDefault().Descendants()
       .Where(x => x.Value<IEnumerable<IPublishedContent>>("tags") != null && x.Value<IEnumerable<IPublishedContent>>("tags").Select(y => y.Id).Contains(1151));
    

    Check for null first.

    HTH

    Steve

  • Steve Morgan 1278 posts 4216 karma points c-trib
    Nov 09, 2020 @ 15:26
    Steve Morgan
    0

    Should probably add this looks to be a quite an expensive query - you're checking all descendants here.

    If this is totally necessary you might want to look at caching this.

    Steve

  • Martin Rud 103 posts 295 karma points
    Nov 09, 2020 @ 17:33
    Martin Rud
    0

    Yes, I guess you are right. The nodes are spread around the site tree so it is necessary.

    After Descendants() I have .Where(x => x.IsDocumentType(articleType.Key)) but I guess that only helps a little.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies