Hi,
I want to get the user roles of the current logged user!
Say User has two roles A and B assigned directly via Manage roles options and have some indirect roles say C assigned via that team that the user belongs too.
Now the User will have three Roles AB(Directly Assigned )C(Indirectly assigned via- Team).
Now I want to get the roles AB(Directly assigned) only. Using "Xrm.Page.context.getUserRoles()" i am getting all the roles the user have.
How to achieve it, any suggestions?
*This post is locked for comments
Thank you Pavan for your prompt response. The above code is working as expected.
Hi,
Use the following code.
function CheckUserRole() { var currentUserId = Xrm.Page.context.getUserId(); var currentUserRoles = getCurrentUserRoles(currentUserId); } // Get Roles of User only function getCurrentUserRoles(currentUserId){ var userId = currentUserId.slice(1, -1); var req = new XMLHttpRequest(); req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/systemuserrolescollection?$select=roleid&$filter=systemuserid eq "+userId+"", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); req.onreadystatechange = function() { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var results = JSON.parse(this.response); for (var i = 0; i < results.value.length; i++) { var userRoleId = results.value[i].roleid; var userRoleName = GetRoleName(userRoleId); } } else { Xrm.Utility.alertDialog(this.statusText); } } }; req.send(); } //Get Rolename based on RoleId function GetRoleName(roleId) { var req = new XMLHttpRequest(); req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/roles("+roleId+")?$select=name", false); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); req.onreadystatechange = function() { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var result = JSON.parse(this.response); var roleName = result["name"]; alert(roleName); } else { Xrm.Utility.alertDialog(this.statusText); } } }; req.send(); }
Note: In case my answer helped you. Kindly mark it as verified.
Warm Regards,
Pavan Kumar Garlapati
Follow my Blog: https://pavankumargarlapati.wordpress.com
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,134 Super User 2024 Season 2
Martin Dráb 229,928 Most Valuable Professional
nmaenpaa 101,156