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!
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, 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...
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?
and my ViewModel...
Do I need to do a lookup based on the IMember ID? There's got to be a better way!
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?
Just here to say that I have this same need - converting a V8 site to V9 and hit this exact snag 😢
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...
is working on a reply...