Copied to clipboard

Flag this post as spam?

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


  • Bijesh Tank 192 posts 420 karma points
    Mar 10, 2016 @ 11:46
    Bijesh Tank
    0

    Find which groups a member belongs to

    Hi,

    How can I get a list of groups that a single (current) member belongs to? I have an instance of IMember but I can't see groups.

    The the old way, you could do something like

    var currentMember = umbraco.cms.businesslogic.member.Member.GetCurrentMember();
    var currentMemberGroups = currentMember.Groups.Values;
    

    Whats the best way using the API?

    Thanks, /B

  • Ravi Motha 290 posts 500 karma points MVP 7x c-trib
    Mar 10, 2016 @ 13:49
    Ravi Motha
    0

    can you not do a foreach on the

    var currentMemberGroups = currentMember.Groups;
    
    foreach (var memGroup in currentMemberGroups)
    {
                       do something with memGroup
    }
    
  • Bijesh Tank 192 posts 420 karma points
    Mar 10, 2016 @ 14:16
    Bijesh Tank
    0

    Problem is currentMember is not an IMember. And IMember doesn't expose Groups.

    The closest thing I could find to getting groups/roles for a member is using System.Web.Security.Roles.GetRolesForUser

  • Ravi Motha 290 posts 500 karma points MVP 7x c-trib
    Mar 10, 2016 @ 15:03
    Ravi Motha
    0

    I was going to say unless you are willing to work the wrong way round, ie.

    get all the roles and then check if the user is in those roles?? though that feels like a pfaff/dirty workaround..

  • Bijesh Tank 192 posts 420 karma points
    Mar 10, 2016 @ 16:15
    Bijesh Tank
    0

    It is quite dirty indeed. Essentially I need the group Id's the member belongs to.

    var memberRoles = System.Web.Security.Roles.GetRolesForUser(member.Username).ToList();
    var memberInGroups = new List<int>();
    foreach (var role in memberRoles)
    {
          var group = MemberGroupService.GetByName(role);
          memberInGroups.Add(group.Id);
    }
    

    Seems to work but probably not the way I wanted to write it.

  • Ravi Motha 290 posts 500 karma points MVP 7x c-trib
    Mar 10, 2016 @ 16:17
    Ravi Motha
    0

    sounds like an API extension issue that needs to be raised.. the list of groups by member

Please Sign in or register to post replies

Write your reply to:

Draft