Copied to clipboard

Flag this post as spam?

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


  • Gordon Saxby 1461 posts 1883 karma points
    May 31, 2022 @ 09:23
    Gordon Saxby
    0

    IMember in Umbraco 9

    I have jumped from v7 to v9 - I am not sure how Members are dealt with now, in code especially in something like an API or Hangfire code where there isn't a logged on member.

    I have been used to using IMember but although it still seems to be available, I'm wondering if MemberIdentityUser is its replacement? Or are members now handled as IPublishedContent?

    Any help would be most appreciated, as would pointers to documentation, blogs, tutorials, etc.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jun 01, 2022 @ 13:48
    Huw Reddick
    0

    Hi Gordon,

    It realy depends what exactly you are trying to do as there are several dependencies you can inject depending what you need to do. Could you provide an example of what you were doing before and I may be able to help

  • Gordon Saxby 1461 posts 1883 karma points
    Jun 01, 2022 @ 14:47
    Gordon Saxby
    0

    For example, I use this in an API controller (based on UmbracoApiController) to get the current Member (which has been authenticated using OAuth):

        MemberIdentityUser member = await _memberManager.GetCurrentMemberAsync();
    

    However, for me to access custom fields on the Member type, it seems as though I have to convert it to an IMember:

            IMember? memberRecord = _memberService.GetById(Convert.ToInt32(member.Id));
            string? publicToken = memberRecord.GetValue<string>("publicTokenMain");
    
  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jun 01, 2022 @ 15:49
    Huw Reddick
    0

    I think you can also convert it to an IPublishedContent to access custom fields.

    var user = _memberManager.GetCurrentMemberAsync().Result;
    IPublishedContent member = _memberManager.AsPublishedMember(user);
    
  • Gordon Saxby 1461 posts 1883 karma points
    Jun 07, 2022 @ 15:26
    Gordon Saxby
    0

    That doesn't seem to help, in fact it seems to make things more difficult?

    Using:

    var user = _memberManager.GetCurrentMemberAsync().Result;
    IPublishedContent member = _memberManager.AsPublishedMember(user);
    

    you then seem to have to access properties like:

    var phoneNumber = member.GetProperty("phoneNumber").GetValue().ToString();
    

    which isn't as simple as accessing it via IMember:

    var pn = member.GetValue<string>("phoneNumber");
    

    If I could get the member data as a ModelsBuilder Member, then it would just be:

    var phoneNumber = memberRecord.PhoneNumber
    

    However, creating a ModelsBuilder Member now requires a fallback value too, but I can't find out anything about what that is!?

    Member(IPublishedContent content, IPublishedValueFallback publishedValueFallback)
    
  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jun 07, 2022 @ 15:52
    Huw Reddick
    1

    This will give you an IMember

    var member= _memberService.GetByKey(user.Key); //where user is MemberIdentityUser
    

    You can create an IMember like

    var member = _memberService.CreateMember(newmember.Email, newmember.Email, newmember.Name, "member");
    
  • Gordon Saxby 1461 posts 1883 karma points
    Jun 07, 2022 @ 16:23
    Gordon Saxby
    0

    Sorry, I think my question is drifting a bit as I do more development!

    I have enabled ModelsBuilder because I used that with Umbraco 7 and it was an easy way to get access to properties (as I mentioned above).

    However, getting an instance of a ModelsBuilder object is not quite so easy as it now requires a "publishedValueFallback" ... but I don't know what that it is, or where it comes from.

    Also, you have to pass it an IPublishedContent version of the data (member). That is what started me wondering whether I should try to only deal with IPublishedContent, or IMember, etc.

    Maybe if I could get an explanation of what "publishedValueFallback" is, where it comes from, what it means, etc then maybe that would help me understand it all better. I have searched Google for "publishedValueFallback" and not found any explanation of it.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jun 07, 2022 @ 16:36
    Huw Reddick
    0

    Hi Gordon,

    I don't know if this will help, but I have had to use the fallbacks in render controllers. I just do the following

     new PublishedValueFallback(_serviceContext, _variationContextAccessor)
    

    _variationContextAccessor and _serviceContext and passed to controller contructor using DI

    IVariationContextAccessor variationContextAccessor,ServiceContext context
    
  • Gordon Saxby 1461 posts 1883 karma points
    Jun 07, 2022 @ 16:41
    Gordon Saxby
    0

    Yes, I have seen that type of code ... but what does it mean?!

    If I am writing an API controller which deals with a number of different data types, then what will be supplied by DI?

    I guess I'll have to try it and then debug to see what happens!?

    Typical Umbraco that it's barely mentioned in the documentation :-(

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jun 07, 2022 @ 17:07
    Huw Reddick
    0

    I believe there is a tryparse method that allows you to pass in a published content object

  • Gordon Saxby 1461 posts 1883 karma points
    Jun 08, 2022 @ 07:59
    Gordon Saxby
    0

    I've had a look and can't see anything like that.

    For now, I think I'll have to just forget about ModelsBuilder and use IMember.

    Thanks for your help anyway.

  • Arul Prabakaran 6 posts 90 karma points c-trib
    Jun 08, 2022 @ 15:47
    Arul Prabakaran
    2

    Hi Gordon,

    Thanks for reaching out the community.

    If you are using ModelsBuilder, then you can use UmbracoContext to cast IMember as Member created by ModelsBuilder.

    This will give you strongly typed Member object from which you can access each property.

    Click here for example

    Paul Seal has a wonderful repository based on Umbraco 9 membership to solve most of the membership related doubts.

    Github repo

Please Sign in or register to post replies

Write your reply to:

Draft