Copied to clipboard

Flag this post as spam?

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


  • Nathan Reece 62 posts 376 karma points
    May 18, 2021 @ 03:55
    Nathan Reece
    0

    Umbraco 8 how to parse IMember to strongly typed MemberType

    Hi All,

    I am wondering how to convery an IMember to a MemberType. So I have access to the strongly typed fields. So I have a member which has a load of custom fields. I use the following code to get the member. IMember member = _memberService.GetByEmail(email); I am just not sure how to get access to the custom fields.

    Thanks.

  • David Armitage 509 posts 2079 karma points
    May 18, 2021 @ 04:17
    David Armitage
    100

    Hi Nathan,

    If you think of a memeber as a content node that will set you on the right path.

    Like with content there is helpers and services. Helpers generally get the data from cache and services get from the DB.

    It looks as though you are using the service when in-fact you are probably wanting to use a helper method instead.

    Try this code

    private readonly MembershipHelper _membershipHelper;
    
    public OnlineBookingMember GetOnlineBookingMember(string email)
            {
                try
                {
                    IPublishedContent member = _membershipHelper.GetByEmail(email);
                    if (member != null && member is OnlineBookingMember onlineBookingMember)
                        return onlineBookingMember;
                }
                catch (Exception e)
                {
                    _logger.Error<BookingBookingMemberHelper>(e, "GetOnlineBookingMember | Exception: {0} | Message: {1}", e.InnerException != null ? e.InnerException.ToString() : "", e.Message != null ? e.Message.ToString() : "");
                }
    
                return null;
            }
    

    Notice how I am using the membership helper to get the member instead of the service. This will bring back IPublishedContent which you can then cast as your member type and have access to all the custom profile fields as strongly typed properties.

    I am sure this will help you.

    Regards

    David

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    May 18, 2021 @ 08:04
    Søren Gregersen
    0

    Hi David,

    A small suggestion for your method:

    public OnlineBookingMember GetOnlineBookingMember(string email)
    {
        return _membershipHelper.GetByEmail(email) as OnlineBookingMember;
    }
    

    You could add the logging, but I don't see that it should fail :)

Please Sign in or register to post replies

Write your reply to:

Draft