Retrieve All Privileges for a Security Role in C#
Views (3313)
To retrieve all privileges for a security role, use RetrieveRolePrivilegesRoleRequest.
var roleRequest = new RetrieveRolePrivilegesRoleRequest { RoleId = new Guid("securityRoleId") }; var roleReponse = service.Execute(roleRequest);
Replace securityRoleId with the ID of the security role you want to query and service with your IOrganizationService.
roleResponse.RolePrivileges will contain a long list of privilege IDs which are not very useful on their own. You can get the name of all of these privileges like this:
var privilegeQuery = new QueryExpression { EntityName = "privilege", ColumnSet = new ColumnSet(true) }; var filter = new FilterExpression(LogicalOperator.Or); foreach (var p in roleResponse.RolePrivileges) filter.AddCondition("privilegeid", ConditionOperator.Equal, p.PrivilegeId); privilegeQuery.Criteria = filter; var privileges = service.RetrieveMultiple(privilegeQuery);
Now you will have all columns (attributes) for all privileges for a given security role stored in privileges
To go one step further, you can filter your privileges by the entity they refer to like so:
var contactPrivileges = privileges.Entities.ToList() .Where(p => p.GetAttributeValue<string>("name").ToLower() .Contains("contact")) .ToList();
This was originally posted here.
*This post is locked for comments