How do I get User Roles based on Username?

UserRoles roles = user.Roles;
string role = roles.GetEnumerator().ToString();

If I have a user, I can get it like this. But how do I get the roles when all I have is a Username?

2 Answers 2


To get a user by a username you can use :

public class UserRepository
    public static User GetUser(string domainName, string userName)
       if (User.Exists(domainName + @"\" + userName))
           return User.FromName(domainName + @"\" + userName, true);
       return null;

To get a user roles you can use

  Sitecore.Security.Accounts.User user = UserRepository.GetUser("yourdomain","youruserName");

  if (user==null) return;

  var rolesList = user.Roles.ToList();

  foreach(var role in rolesList)
     var roleName = role.DisplayName;
     var roleDomain = role.Domain;
     // you have all the properties of a role in role object
  • 2
    Nice! Small comment... since your example above can return null for a user, you probably should have a check before accessing user.Roles Commented Aug 4, 2017 at 12:36

Perhaps you want to extract a list of users and their roles into a report.

$users = Get-User -Filter * | Where-Object { $_.Roles.Count -gt 0 }

$records = [System.Collections.ArrayList]@()
foreach($user in $users) {
    $record = [PSCustomObject]@{
        "Username" = $user.Name
        "Roles" = ($user.Roles | Select-Object -ExpandProperty Name) -join ","
    $records.Add($record) > $null


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.