Friday, 30 November 2018

Getting Security Role Name for logged-in user in Dynamics using Xrm.

get logged in user role in dynamic crm
Description:

In this example, we explain that how to get logged in user role in Dynamic 365 CRM.or how to get security role name for logged-in user in Dynamic CRM using XRM.or how to retrieving login user security roles in Dynamic CRM.or how to get role name of current user in Dynamic CRM using JavaScript. Alternatively, get current logged in user’s security role using JavaScript.so below is the JavaScript code with XRM will used to get or retrieve current logged in user role in Dynamic CRM.
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();
       }


1 comments: