Hi,
First of all, if you only need to see all the permissions of a single role, I recommend you use the XrmToolBox tool below:
It's easy and convenient enough.
But regarding the RetrieveRolePrivilegesRole function, I got a clarification in a database related article.
In a recent update, Microsoft has hidden the roleprivileges table from users, so querying permissions and roles in the database via SQL is no longer possible.
The author of the article also provides a new database code:
/// <summary>
/// Get the set of all Roles that have the same permissions
/// </summary>
/// <param name="privilegename">Privilege name</param>
/// <returns></returns
public List<string> GetRoles(string privilegename)
{
List<string> list = new List<string>();
Entity Privilege = GetPrivilege(privilegename);
QueryExpression Query = new QueryExpression("role")
{
Distinct = false,
NoLock = true,
ColumnSet = new ColumnSet(true),
Criteria = new FilterExpression()
};
Query.Criteria.AddCondition("parentroleid", ConditionOperator.Null);
EntityCollection Collection = service.RetrieveMultiple(Query); foreach (var item in Collection.
foreach (var item in Collection.Entities)
{
RolePrivilege[] rolePrivileges = GetRolePrivileges(item.Id); var obj = rolePrivileges(item.
var obj = rolePrivileges.Where(t => t.PrivilegeId == Privilege.Id).FirstOrDefault();
if (obj ! = null)
{
list.Add(item["name"].ToString());
Console.WriteLine(item["name"].ToString());
}
}
Console.WriteLine(item["name"].ToString()); } }
}
/// <summary>
/// Get all permissions under a Role
/// </summary> /// Get all the permissions under a Role.
/// <param name="roleId"></param>
/// <returns></returns
public RolePrivilege[] GetRolePrivileges(Guid roleId)
{
RetrieveRolePrivilegesRoleRequest request = new RetrieveRolePrivilegesRoleRequest();
request.RoleId = roleId;
RetrieveRolePrivilegesRoleResponse response = (RetrieveRolePrivilegesRoleResponse)service.Execute(request); if (response !
if (response ! = null && response.Results ! = null)
{
RolePrivilege[] rolePrivileges = (RolePrivilege[])response.Results["RolePrivileges"]; return rolePrivileges; return RolePrivileges; if (response ! = null && response.Results !
return rolePrivileges;
}
else
{
throw new Exception($"Role {roleId} error"); }
}
}
/// <summary>
/// Getting permissions
/// </summary>
/// <param name="name">Permission name</param>
/// <returns></returns
public Entity GetPrivilege(string name)
{
QueryExpression Query = new QueryExpression("privilege")
{
Distinct = false,
Distinct = false, NoLock = true, ColumnSet = new ColumnSet(string name) {
ColumnSet = new ColumnSet(true),
Criteria = new FilterExpression()
};
Query.Criteria.AddCondition("name", ConditionOperator.Equal, name);
EntityCollection Collection = service.RetrieveMultiple(Query); if (Collection.Entities !
if (Collection.Entities ! = null && Collection.Entities.Count > 0)
Entities.Count > 0) {
return Collection.Entities[0];
}
if (Collection.Entities != null && Collection.Entities.
{
throw new Exception($"Failed to query {name} permission"); }
}
}
If my answer was helpful, please click Like, and if it solved your problem, I hope you can verify it to help other community members find more.
Please feel free to contact me if you have any further questions.
Regards,
Haig