Hi Experts
I used a JavaScript to get logged in users security role using a JavaScript on CRM2015, but now we upgraded to CRM2016. And the form is throwing and error. Below is the code I'm using, please advice.
function FormOnLoad() { var optCtrl = Xrm.Page.ui.controls.get("statuscode"); var CMRole = CheckUserRole("System Administrator"); if (Xrm.Page.ui.getFormType() == 6 && !CMRole) { optCtrl.removeOption(100000000); } } function GetServerUrl() { return Xrm.Page.context.getClientUrl() + "/xrmservices/2011/organizationdata.svc/"; } function CheckUserRole(roleName) { var currentUserRoles = Xrm.Page.context.getUserRoles(); for (var i = 0; i < currentUserRoles.length; i++) { var userRoleId = currentUserRoles[i]; var userRoleName = GetRoleName(userRoleId); if (userRoleName == roleName) { return true; } } return false; } function GetRoleName(userRoleId) { var selectQuery = "RoleSet?$top=1&$filter=RoleId eq guid'" + userRoleId + "'&$select=Name"; var odataSelect = GetServerUrl() + selectQuery; //alert(odataSelect); 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) { var result = data.d; if (!!result) { roleName = result.results[0].Name; } }, error: function (XmlHttpRequest, textStatus, errorThrown) { //alert('OData Select Failed: ' + odataSelect); } }); return roleName; }
Error Thrown is:
*This post is locked for comments
Ben,
Cool approach and thanks for heads up but back in 2017 when I answered this question - method you used was not available and was introduced around 2 years ago.
For better performance, try this pattern instead of a service call to the API...
community.dynamics.com/.../get-user-security-role-name-from-context-in-dynamics-365-ce-crm
//you can user this script
function CheckUserRole(roleName) {
var currentUserRoles = Xrm.Page.context.getUserRoles();
for (var i = 0; i < currentUserRoles.length; i++) {
var userRoleId = currentUserRoles[i];
var theUserRoleName = GetTheRoleName(userRoleId);
if (theUserRoleName == roleName) {
return true;
}
//debugger;
}
return false;
}
function GetRoleName (userRoleId) {
var roleName = "";
var columns = ["Name"];
var fileObj = CrmRestKit.Retrieve("Role", userRoleId, columns, false);
if (fileObj.responseJSON.d != null) {
var data = fileObj.responseJSON.d;
roleName = data.Name;
}
return roleName;
}
Thanks a lot Andrew. it seems to solve the issue.
Hi,
As suggested by Ben, in the attached link by Andrew, we usually use the below code
if (typeof($) === 'undefined') { $ = parent.$; jQuery = parent.jQuery; }
Hope it helps.
Are you using XRMServiceToolkit?
If you are, your please check the version of the toolkit you are using. Starting with 2015 Update 1, you will need to use at least version 2.2.1 of the XRMServiceToolkit.
Hope this helps.
Hello,
Just curious - have you tried to check issue you experience in search engine? I was able to find an answer less than in 1 minute - community.dynamics.com/.../168698
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 290,902 Super User 2024 Season 2
Martin Dráb 229,316 Most Valuable Professional
nmaenpaa 101,156