Copied to clipboard

Flag this post as spam?

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


  • Garret 68 posts 308 karma points
    Jul 20, 2020 @ 18:17
    Garret
    0

    Getting nested content element of Member

    Hi All,

    I've added a nested content property to a member.

    In my UmbracoAPIController when I want to 'fetch' that property, with the following code I get null back.

    member.GetValue<IEnumerable<IPublishedElement>>("challenges");

    Where member is of type IMember

    But when I debug my code and use member.GetValue("challenges"); i get do get the element back.

    [{"key":"77eb3ad2-af6d-486c-82e7-dadc0b254147","name":"Item 1","ncContentTypeAlias":"memberChallenge","challenge":"umb://document/429a0370a1a0471f8a5f34cf26023614","dateEnrolled":"2020-05-11T00:00:00","completed":0}]
    

    So it looks like somehow Umbraco isn't able to parse the element. Or maybe I'm doing something wrong, which is the more logical option

  • Garret 68 posts 308 karma points
    Jul 20, 2020 @ 18:53
    Garret
    0

    Testing with member.HasProperty("challenges") returns true

    trying to cast the value (IEnumerable<IPublishedElement>)cursistMember.GetValue("challenges") results in an invalid cast exception

  • Garret 68 posts 308 karma points
    Jul 20, 2020 @ 19:21
    Garret
    0

    To make it a little stranger...if I get the Member as IPublishedContent first

    var memberContent = umbracoHelper.Member(member.Id);
    

    and use the methods of IPublishedContent to get the property

    memberContent.Value<IEnumerable<IPublishedElement>>("challenges");
    

    It suddenly works!?

  • Nik 1599 posts 7178 karma points MVP 6x c-trib
    Jul 20, 2020 @ 22:03
    Nik
    100

    Hi Garret,

    The reason you are seeing this behaviour is that IMember / IContent behave differently to the "Published" version.

    IContent/IMember's .GetValue method is returning the raw json array string and has no understanding of PropertyValueConverters at this point. If you wanted to get the strongly typed version you would need to get the right property value converter and pass the raw string value in to get the right value out.

    Does that make sense?

    Nik

  • Garret 68 posts 308 karma points
    Jul 21, 2020 @ 07:48
    Garret
    0

    I had the realisation last night that you need a PropertyValueConverter and that GetValue<> of IContent/IMember probably doesn't have a PropertyValueConverter for NestedContent (or IEnumerable

    But I do see a more than one use case for having a NestedContent property for a member (enrolled in certain entities, documents belonging to a member, etc, etc). So would be great if IMember would have the PropertyValueConverter for IEnumerable

Please Sign in or register to post replies

Write your reply to:

Draft