Retrieve the List of Security Roles Assigned for a Logged-In Dynamics 365 User

Introduction
 
In this blog, I will demonstrate how to get the Dynamics CRM User’s security role using JavaScript.
 
Retrieving the current user logged in to Dynamics 365
 
The function 'RetrieveLoggedInD365UserSecurityRoles()' gets the current global context that contains the user settings. There is a property in user settings for retrieving security roles GUIDs (userSettings.securityRoles) and the GUIDs are stored in an array 'loggedInUserSecurityRolesArray'. With those GUIDs, we can query Role entity to get the security role names using FetchXML. We are using FetchXML_GetRecords(originalFetch, entityname) function to retrieve the security role names from Role entity.
 
JavaScript
  1. //***********To get the LoggedIn D365 User’s Security Role**************//    
  2.     
  3. function RetrieveLoggedInD365UserSecurityRoles() {  
  4. var resultset = "";    
  5. var fetchXMLCondition = "";    
  6. // Get Logged In D365 User’s Context    
  7. var userSettings = Xrm.Utility.getGlobalContext().userSettings;    
  8. // Get Logged In User Security Roles GUIDs   
  9. var loggedInUserSecurityRolesArray = userSettings.securityRoles;  
  10. var totalSecurityRolesArray = new Array();  
  11. var rolesOutputText = "";  
  12. if (loggedInUsersecurityRolesArray.length > 0) {  
  13. var i;  
  14. // Framing the conditions for FetchXML to get Security Role names using the Security Roles GUIDs  
  15. for (i = 0; i < loggedInUsersecurityRolesArray.length; i++) {  
  16. fetchXMLCondition += "<condition attribute='roleid' operator='eq' value='" + loggedInUsersecurityRolesArray[i] + "'/>";  
  17. }  
  18. var fetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +   
  19. "<entity name='role'>" +  
  20. "<attribute name='roleid' />" +  
  21. "<attribute name='name' />" +  
  22. "<order attribute='name' descending='false' />" +  
  23. "<filter type='or'>" + fetchXMLCondition +  
  24. "</filter>" +  
  25. "</entity>" +  
  26. "</fetch>";  
  27. resultset = FetchXML_GetRecords(fetchXML, "roles");  
  28. }  
  29. // returns the resultset that has the security roles assigned for the current logged in user   
  30. return resultset;  
  31. }  
  32.   
  33. //***************FetchXML to get the result set***********************//    
  34. function FetchXML_GetRecords(originalFetch, entityname) {  
  35. var records;  
  36. var fetch = encodeURI(originalFetch);  
  37. var serverURL = Xrm.Page.context.getClientUrl();  
  38. var Query = entityname + "?fetchXml=" + fetch;  
  39. var req = new XMLHttpRequest();  
  40. req.open("GET", serverURL + "/api/data/v9.0/" + Query, false);  
  41. req.setRequestHeader("OData-MaxVersion""4.0");  
  42. req.setRequestHeader("OData-Version""4.0");  
  43. req.setRequestHeader("Accept""application/json");  
  44. req.setRequestHeader("Content-Type""application/json; charset=utf-8");  
  45. req.setRequestHeader("Prefer""odata.include-annotations=\"*\"");  
  46. req.onreadystatechange = function () {  
  47. if (this.readyState === 4) {  
  48. req.onreadystatechange = null;  
  49. if (this.status === 200) {  
  50. var results = JSON.parse(this.response);  
  51. if (results != null) {  
  52. records = results.value;  
  53. }  
  54. else {   
  55. Xrm.Utility.alertDialog(this.statusText);  
  56. }  
  57. }   
  58. };   
  59. req.send();  
  60. return records;  
  61. };  
To fetch records using FetchXML
 
The JavaScript function FetchXML_GetRecords(originalFetch, entityname) that receives FetchXML (originalFetch) and the entity name (entityname) on which the FetchXML has to be run against.
 
Output: ResultSet
 
System Administrator
Sales Person
Sales Manager
Support Professional
Supervisor
 
Summary
 
In my next blog, I will describe how to make use of a security role to show or hide Fields in the Entity Form.