Ginger Squirrel
Ginger Squirrel

Reputation: 663

How do I list all member groups in Umbraco?

I am currently trying to create a page where I have a list of all the groups in the the Umbraco member area.

Most of what is returned when I google for an answer is how to get all the groups for a specific user, however this is not what I want, I would like all the groups listed that are available for all members.

I guess the alternative is to create a user that has permission for all the groups and then loop through that member's groups, but this would require me to ensure that this member is added to every new group I create.

Upvotes: 1

Views: 2015

Answers (2)

harvzor
harvzor

Reputation: 2908

There is a method on the MemberService which gets all of the Umbraco Member roles:

var roles = ApplicationContext.Current.Services.MemberService.GetAllRoles();

Read about this method here.

I can't figure out from looking at the source of Umbraco if this method just uses System.Web.Security.Roles.GetAllRoles(). I would probably recommend using the Umbraco method as it allows you to switch out the default membership provider without needing to change all of the method calls in the views.

Upvotes: 4

Ginger Squirrel
Ginger Squirrel

Reputation: 663

I have found the answer for anyone looking (Thank-you Stack Overflow for your rubber duck effect!)

@{
  string[] Roles = System.Web.Security.Roles.GetAllRoles();
}
<ul>
  @foreach (string role in Roles) {
    <li>@role</li>
  }
</ul>

Upvotes: 2

Related Questions