Get Record's User Security Roles in CRM 2015 using Javascript
function GetUserRoles() {
var roles = Xrm.Page.context.getUserRoles();
for (var i = 0; i < roles.length; i++) {
GetRole(roles[i]);
}
}
function GetRole(roleid) {
var serverUrl = Xrm.Page.context.getClientUrl();
var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/RoleSet?$select=Name&$filter=RoleId eq guid'" + roleid + "'";
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 () {
GetRoleData(this);
};
retrieveReq.send();
}
function GetRoleData(retrieveReq) {
if (retrieveReq.readyState == 4) {
if (retrieveReq.status == 200) {
var retrieved = JSON.parse(retrieveReq.responseText).d;
if(retrieved.results[0].Name="System Administrator")
{
Xrm.Page.getAttribute("cms_showdialogbutton").setValue(0);
}
else
{
Xrm.Page.getAttribute("cms_showdialogbutton").setValue(1);
}
alert(retrieved.results[0].Name);//Security Role Name
}
}
}
function GetUserRoles() {
var roles = Xrm.Page.context.getUserRoles();
for (var i = 0; i < roles.length; i++) {
GetRole(roles[i]);
}
}
function GetRole(roleid) {
var serverUrl = Xrm.Page.context.getClientUrl();
var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/RoleSet?$select=Name&$filter=RoleId eq guid'" + roleid + "'";
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 () {
GetRoleData(this);
};
retrieveReq.send();
}
function GetRoleData(retrieveReq) {
if (retrieveReq.readyState == 4) {
if (retrieveReq.status == 200) {
var retrieved = JSON.parse(retrieveReq.responseText).d;
if(retrieved.results[0].Name="System Administrator")
{
Xrm.Page.getAttribute("cms_showdialogbutton").setValue(0);
}
else
{
Xrm.Page.getAttribute("cms_showdialogbutton").setValue(1);
}
alert(retrieved.results[0].Name);//Security Role Name
}
}
}