Copied to clipboard

Flag this post as spam?

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


  • Jamie Attwood 201 posts 493 karma points c-trib
    Apr 07, 2022 @ 22:13
    Jamie Attwood
    0

    How to Cast IMember as IPublishedContent OR Models Builder Typed Version of Member

    I know that MembershipHelper is no longer a thing in V9. But I need to get a list of all members of a certain role. So I am using the dreaded memberservice _memberService.FindMembersInRole() to this. That all is returning fine however, now for the life of me I can't cast IMember to either IPublishedContent Or the strongly typed version of that member type. Everything works until the cast happens, and then its cast to Null...

    How can I cast an IMember to either IPublishedContent or the Modelsbuilder version of that IPub?

            var model = new AdminDashboardViewModel();
            var allAdvisors = _memberService.FindMembersInRole("Advisors", "", matchType: Umbraco.Cms.Core.Persistence.Querying.StringPropertyMatchType.StartsWith);
            if (allAdvisors?.Any() ?? false)
            {
                List<IPublishedContent> advisorList = new List<IPublishedContent>();
                foreach (var advisor in allAdvisors)
                {
                    IPublishedContent castAdvisor = advisor as IPublishedContent;
                    advisorList.Add(castAdvisor);
                }
                model.Advisors = advisorList;
            }
    

    and my ViewModel...

    public class AdminDashboardViewModel
    {
        public List<IPublishedContent> Advisors { get; set; }
    }
    

    Do I need to do a lookup based on the IMember ID? There's got to be a better way!

  • Jamie Attwood 201 posts 493 karma points c-trib
    Apr 08, 2022 @ 13:25
    Jamie Attwood
    0

    An ugly, ugly update to my own question. This is possible but I am sure this is highly non-performant. First, get a list of all members by role as IMember, next lookup the member as an MemberIdentityUser via membermanager, then use the AsPublishedMember() method to get it's Ipublished content representation. Man, there has to be a better way. Any suggestions?

    var model = new AdminDashboardViewModel();
                var allAdvisors = _memberService.FindMembersInRole("Advisors", "", matchType: Umbraco.Cms.Core.Persistence.Querying.StringPropertyMatchType.StartsWith);
                if (allAdvisors?.Any() ?? false)
                {
                    List<IPublishedContent> advisorList = new List<IPublishedContent>();
                    foreach (var advisor in allAdvisors)
                    {
                        MemberIdentityUser memberById = await _memberManager.FindByEmailAsync(advisor.Email);
                        IPublishedContent memberAsContent = _memberManager.AsPublishedMember(memberById);
                        advisorList.Add(memberAsContent);
                    }
                    model.Advisors = advisorList;
                }
    
  • Myke Bates 11 posts 53 karma points
    May 15, 2022 @ 05:10
    Myke Bates
    0

    Just here to say that I have this same need - converting a V8 site to V9 and hit this exact snag 😢

  • Jamie Attwood 201 posts 493 karma points c-trib
    May 16, 2022 @ 13:27
    Jamie Attwood
    0

    Myke, I am going to add a bug/feature request. We need membership helper in v9 or a more robust memberManager. I do see a few not implemented exceptions in the user store so maybe this is related. I will let you know the link so you can upvote it...

Please Sign in or register to post replies

Write your reply to:

Draft