How to get "Current User Role and Role Name" in CRM for roll-up 14.
Hello CRM lovers, Here is my blog for getting logged user Role and Role name for Roll-Up 12, Roll-Up 13, Roll-Up 14 and also in cross browser support. I will really help you. For achieving this you have to fallow these steps.
Step 1:- First of all you have to add reference of J Query main file minimum version jquery1.4.1.min or higher version. you can download it from http://jquery.com/.
Step 2:- Now you have to add reference of "json2" on your crm entity form, You can get it from sdk, you will find it in your sdk folder.
Step 3:- Now add the fallowing code in your web resource.
//*******Retrive Multiple recored on the basis of the criteria OData Rest Method************
//******************************************************************************************
function retrieveMultiple(odataSetName, filter, successCallback, errorCallback, _executionObj) {
_executionObjMultiretrive = _executionObj;
var context = Xrm.Page.context;
var serverUrl = context.getServerUrl();
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
//odataSetName is required, i.e. "AccountSet"
if (!odataSetName) {
alert("odataSetName is required.");
return;
}
//Build the URI
var odataUri = serverUrl + ODATA_ENDPOINT + "/" + odataSetName;
//If a filter is supplied, append it to the OData URI
if (filter) {
odataUri += filter;
}
//Asynchronous AJAX function to Retrieve CRM records using OData
$.ajax({
type: "GET",
async: false,
contentType: "application/json; charset=utf-8",
datatype: "json", url: odataUri,
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
if (successCallback) {
if (data && data.d && data.d.results) {
successCallback(data.d.results, textStatus, XmlHttpRequest);
}
else if (data && data.d) {
successCallback(data.d, textStatus, XmlHttpRequest);
}
else {
successCallback(data, textStatus, XmlHttpRequest);
}
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
if (errorCallback)
errorCallback(XmlHttpRequest, textStatus, errorThrown);
else
errorHandler(XmlHttpRequest, textStatus, errorThrown);
}
});
}
function errorHandler(xmlHttpRequest, textStatus, errorThrow) {
alert("Error : " + textStatus + ": " + xmlHttpRequest.statusText);
}
Step 4 :- Now add the fallowing three functions in your web resource, In third function you will get current logged user role name.
//**********************************************************************************
//***************** Function For getting the Current User Role********************
//**********************************************************************************
function getOwnerRole(UserID) {
var _Userid = UserID;
if (_Userid != null) {
var istrue = retrieveMultiple("SystemUserRolesSet", "?$filter=SystemUserId eq (guid'" + _Userid + "')", RetrieveDataFromSystemUser, null, null);
}
}
function RetrieveDataFromSystemUser(data, textStatus, XmlHttpRequest) {
//debugger;
var executioncontext = _executionObjMultiretrive;
var len = data.length;
if (len > 0) {
for (i = 0; i < len; i++) {
if (data[i].RoleId != null) {
//****************************************************************
//*** This is the first role user have
//*** User may have many roles so you can get all here during loop //******************************************************************************
var userRoleId=data[0].RoleId;
retrieveMultiple("RoleSet", "?$filter=RoleId eq (guid'" + data[i].RoleId + "')", RetrieveRoleName, null, null);
}
}
}
}
function RetrieveRoleName(data, textStatus, XmlHttpRequest) {
//debugger;
var executioncontext = _executionObjMultiretrive;
var len = data.length;
if (data[0].Name != null) {
var userRoleName=data[0].Name;
}
}
//***************** Function For getting the Current User Role********************
//**********************************************************************************
function getOwnerRole(UserID) {
var _Userid = UserID;
if (_Userid != null) {
var istrue = retrieveMultiple("SystemUserRolesSet", "?$filter=SystemUserId eq (guid'" + _Userid + "')", RetrieveDataFromSystemUser, null, null);
}
}
function RetrieveDataFromSystemUser(data, textStatus, XmlHttpRequest) {
//debugger;
var executioncontext = _executionObjMultiretrive;
var len = data.length;
if (len > 0) {
for (i = 0; i < len; i++) {
if (data[i].RoleId != null) {
//****************************************************************
//*** This is the first role user have
//*** User may have many roles so you can get all here during loop //******************************************************************************
var userRoleId=data[0].RoleId;
retrieveMultiple("RoleSet", "?$filter=RoleId eq (guid'" + data[i].RoleId + "')", RetrieveRoleName, null, null);
}
}
}
}
function RetrieveRoleName(data, textStatus, XmlHttpRequest) {
//debugger;
var executioncontext = _executionObjMultiretrive;
var len = data.length;
if (data[0].Name != null) {
var userRoleName=data[0].Name;
}
}
So by fallowing all upper steps you can get current logged user Role Id and Role Name. It will work in cross browser and in all new Roll-Up 12,Roll-Up 13 and Roll-Up 14.
Enjoy the great technology Dynamics CRM. Good luck.....!!!!!
Thanks.
No comments:
Post a Comment