Copied to clipboard

Flag this post as spam?

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


  • Rick Nieling - Perplex 5 posts 88 karma points
    Nov 09, 2021 @ 14:34
    Rick Nieling - Perplex
    1

    Access custom member type properties ModelsBuilder strongly typed

    I am trying to make a page where a logged in user can edit his account details. In the backoffice I created a custom Member Type called "Gebruiker" with some custom member properties, like Name, Last name, Address etc..

    However, whenever i try to access the currently active user's properties through the MemberService:

    var actieveUser = Services.MemberService.GetByUsername(Membership.GetUser().UserName);
    

    The activeUser only contains the 'standard' member properties, like email and name, and not the properties from my "Gebruiker" Member Type. By using the GetValue Method i am able to get my property values, but i would like to achieve this through the strongly typed models instead of property's alias as a string

    var naam = actieveUser.GetValue("Naam");
    var postcode = actieveUser.GetValue("Postcode");
    

    Is there a way to access the active user and instantiate a "Gebruiker" object from which i can access all my custom and standard member properties? For example:

    var naam = actieveUser.Naam;
    
  • Corné Hoskam 81 posts 588 karma points MVP 3x c-trib
    Nov 09, 2021 @ 15:21
    Corné Hoskam
    101

    Hi Rick,

    The Umbraco ModelsBuilder doesn't serialize Members, so what I would recommend is creating either a wrapper class or extension method on the "GetByUsername()" method, in which you define a custom Member model with your custom properties to pass your fetched member into. In your constructor you can then store the GetValue() calls so that, while it's not strongly typed, it's only one model that ever references the values by string, and allows you to work with your own custom Model throughout the rest of the application!

    Kind regards,

    Corné

  • Rick Nieling - Perplex 5 posts 88 karma points
    Nov 10, 2021 @ 10:57
    Rick Nieling - Perplex
    2

    Hi Corné,

    Thanks for the insight . I managed to get the basics to work through Get & SetValue() calls, but i like your suggestion of a wrapper class more. Ill try to set up something like that.

    Cheers!

    Rick

Please Sign in or register to post replies

Write your reply to:

Draft