Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

how to use RetrieveRolePrivilegesRole Function in web api? RetrieveRolePrivilegesRole Function

(0) ShareShare
ReportReport
Posted on by 60
I want to retrieve all the privileges that are assigned to the specified role. So, I am using the RetrieveRolePrivilegesRole Function.
 
I tried using the above function in a REST API:
 
{organization_url}/api/data/v9.0/RetrieveRolePrivileges(RoleId=4931681d-8163-e811-a965-000d3a11fe32)
 
But, it is giving me an error:
 
/Resource not found for the segment 'RetrieveRolePrivileges'./
 
How can I resolve this error?
  • Suggested answer
    Haig Liu Profile Picture
    Haig Liu Microsoft Employee on at
    how to use RetrieveRolePrivilegesRole Function in web api? RetrieveRolePrivilegesRole Function
    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

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,759 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,468 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans