Search This Blog
Tuesday, August 30, 2016
Tuesday, March 15, 2016
Get Record's User Security Roles in CRM 2015 using Javascript
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
}
}
}
Get Current Login User Security Role Name in CRM 2015 using javascript
Get Current Login User Security Role Name in CRM 2015 using javascript
function CheckUserRole() {
var currentUserRoles = Xrm.Page.context.getUserRoles();
for (var i = 0; i < currentUserRoles.length; i++) {
var userRoleId = currentUserRoles[i];
var userRoleName = GetRoleName(userRoleId);
alert(userRoleName );// Security role name popout
}
return false;
}
//Get Rolename based on RoleId
function GetRoleName(roleId) {
//var serverUrl = Xrm.Page.context.getServerUrl();
var serverUrl = location.protocol + "//" + location.host + "/" + Xrm.Page.context.getOrgUniqueName();
var odataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc" + "/" + "RoleSet?$filter=RoleId eq guid'" + roleId + "'";
var roleName = null;
$.ajax(
{
type: "GET",
async: false,
contentType: "application/json; charset=utf-8",
datatype: "json",
url: odataSelect,
beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
success: function (data, textStatus, XmlHttpRequest) {
roleName = data.d.results[0].Name;
},
error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + textStatus + errorThrown + odataSelect); }
}
);
return roleName;
}
function CheckUserRole() {
var currentUserRoles = Xrm.Page.context.getUserRoles();
for (var i = 0; i < currentUserRoles.length; i++) {
var userRoleId = currentUserRoles[i];
var userRoleName = GetRoleName(userRoleId);
alert(userRoleName );// Security role name popout
}
return false;
}
//Get Rolename based on RoleId
function GetRoleName(roleId) {
//var serverUrl = Xrm.Page.context.getServerUrl();
var serverUrl = location.protocol + "//" + location.host + "/" + Xrm.Page.context.getOrgUniqueName();
var odataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc" + "/" + "RoleSet?$filter=RoleId eq guid'" + roleId + "'";
var roleName = null;
$.ajax(
{
type: "GET",
async: false,
contentType: "application/json; charset=utf-8",
datatype: "json",
url: odataSelect,
beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
success: function (data, textStatus, XmlHttpRequest) {
roleName = data.d.results[0].Name;
},
error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + textStatus + errorThrown + odataSelect); }
}
);
return roleName;
}
Subscribe to:
Posts (Atom)