Posted in Code Snippets, CRM, CRM 2016, JavaScripts, OData Call

Checking user has a Particular role

Checking user has a Particular role :

In this article ,explaining the logged in user has a ” Exide POS User ” Security Role or not.

// JavaScript source code
function check() {
 var flag = userroles();
 if (flag == true)
 alert("User has Exide Pos User Role.");
 else
 alert("User has doesn't have Exide Pos User Role.");
}

function userroles() {
 debugger;
 var flag = false;
 var roles = [];
 roles = Xrm.Page.context.getUserRoles();
 var SecurityRoles = [];
 for (var i = 0; i < roles.length; i++) {
 SecurityRoles[i] = Roles(roles[i]);
 if (SecurityRoles[i] == "Exide POS User") {
 flag = true;
 }
 }
 return flag;
}

function getrole(roleid) {
 debugger;
 var security_Names;
 var serverUrl = "/" + Xrm.Page.context.getOrgUniqueName();
 var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
 var ODATA_EntityCollection = "/RoleSet?$select=Name&$filter=RoleId eq guid'" + roleid + "'"

var oDataSelect = serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection;

var retrieveReq = new XMLHttpRequest();
 retrieveReq.open("GET", oDataSelect, false);
 retrieveReq.setRequestHeader("Accept", "application/json");
 retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
 retrieveReq.onreadystatechange = function () {
 security_Names = GetRoleData(this);
};
 retrieveReq.send();
 return security_Names;
}

function GetRoleData(retrieveReq) {
 debugger;
 if (retrieveReq.readyState == 4) {
 if (retrieveReq.status == 200) {
 var retrieved = JSON.parse(retrieveReq.responseText).d;
 return retrieved.results[0].Name;
 }
 }
}

 

That’s It. Enjoy 🙂

 

Advertisements