Wednesday, April 11, 2012

How to find roles of the logged-in user in MS CRM 2011 using JavaScript

MS CRM 2011 provide one method to find all the roles of logged in user, method is:
Xrm.Page.context.getUserRoles()
but it returns only guid's of all the assigned roles no role name but according my requirement I will show/hide some tabs based on role.
We can do it with two ways:

1) using XML Http request

function OnLoad() {
if (UserHasRole("Customer Service Representative") || UserHasRole("Service Manager")) {
Xrm.Page.ui.tabs.get('details').setVisible(false);
Xrm.Page.ui.tabs.get('administration').setVisible(false);
Xrm.Page.ui.tabs.get('contacts').setVisible(false);
} else {
Xrm.Page.ui.tabs.get('details').setVisible(true);
Xrm.Page.ui.tabs.get('administration').setVisible(true);
Xrm.Page.ui.tabs.get('contacts').setVisible(true);
}
}

function UserHasRole(roleName) {
var serverUrl = Xrm.Page.context.getServerUrl();
var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";

oDataEndpointUrl += "RoleSet?$top=1&$filter=Name eq '" + roleName + "'";

var service = GetRequestObject();

if (service != null) {
service.open("GET", oDataEndpointUrl, false);
service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
service.setRequestHeader("Accept", "application/json, text/javascript, */*");
service.send(null);

var requestResults = eval('(' + service.responseText + ')').d;

if (requestResults != null && requestResults.results.length == 1) {
var role = requestResults.results[0];

var id = role.RoleId;

var currentUserRoles = Xrm.Page.context.getUserRoles();

for (var i = 0; i < currentUserRoles.length; i++) {
var userRole = currentUserRoles[i];
if (GuidsAreEqual(userRole, id)) {
return true;
}
}
}
}

return false;
}

function GetRequestObject() {
if (window.XMLHttpRequest) {
return new window.XMLHttpRequest;
} else {
try {
return new ActiveXObject("MSXML2.XMLHTTP.3.0");
} catch (ex) {
return null;
}
}
}

function GuidsAreEqual(guid1, guid2) {
var isEqual = false;

if (guid1 == null || guid2 == null) {
isEqual = false;
} else {
isEqual = guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase();
}

return isEqual;
}

2) By Using Soap request

function OnLoad_ShowHideTabRoleWise() {
if (UserHasRoleByRoleId("Customer Service Representative") || UserHasRoleByRoleId("Service Manager")) {
//hide the tabs
Xrm.Page.ui.tabs.get('details').setVisible(false);
Xrm.Page.ui.tabs.get('administration').setVisible(false);
Xrm.Page.ui.tabs.get('contacts').setVisible(false);
} else {
//show the tabs
Xrm.Page.ui.tabs.get('details').setVisible(true);
Xrm.Page.ui.tabs.get('administration').setVisible(true);
Xrm.Page.ui.tabs.get('contacts').setVisible(true);
}
}

function UserHasRoleByRoleId(roleName) {
//return the list of logged in user all roles guid
var currentUserRoles = Xrm.Page.context.getUserRoles();

for (var i = 0; i < currentUserRoles.length; i++) {
var userRole = currentUserRoles[i];
//compare two roles
if (GuidsAreEqual(WSRetrieveMultiple("role","name","roleid",userRole), roleName)) {
return true;
}
}
return false;
}

function GuidsAreEqual(guid1, guid2) {
var isEqual = false;

if (guid1 == null || guid2 == null) {
isEqual = false;
} else {
isEqual = guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase();
}

return isEqual;
}


function WSRetrieveMultiple(crmEntity, resultAttribute, queryAttribute, queryValue) {
var resultXml;
var oXMLSoapMsg = "<?xml version='1.0' encoding='utf-8'?>"+
"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'"+
" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+
GenerateAuthenticationHeader().toString()+
"<soap:Body>"+
"<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
"<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'"+
" xsi:type='q1:QueryExpression'>"+
"<q1:EntityName>"+crmEntity+"</q1:EntityName>"+
"<q1:ColumnSet xsi:type='q1:ColumnSet'>"+
"<q1:Attributes>"+
"<q1:Attribute>"+resultAttribute+"</q1:Attribute>"+
"</q1:Attributes>"+
"</q1:ColumnSet>"+
"<q1:Distinct>false</q1:Distinct>"+
"<q1:Criteria>"+
"<q1:FilterOperator>And</q1:FilterOperator>"+
"<q1:Conditions>"+
"<q1:Condition>"+
"<q1:AttributeName>"+queryAttribute+"</q1:AttributeName>"+
"<q1:Operator>Like</q1:Operator>"+
"<q1:Values>"+
"<q1:Value xsi:type='xsd:string'>"+queryValue+"</q1:Value>"+
"</q1:Values>"+
"</q1:Condition>"+
"</q1:Conditions>"+
"</q1:Criteria>"+
"</query>"+
"</RetrieveMultiple>"+
"</soap:Body>"+
"</soap:Envelope>";
var oXMLHTTPReq = new ActiveXObject("Msxml2.XMLHTTP");
oXMLHTTPReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
oXMLHTTPReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
oXMLHTTPReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
oXMLHTTPReq.setRequestHeader("Content-Length", oXMLSoapMsg.length);
oXMLHTTPReq.send(oXMLSoapMsg);
resultXml = oXMLHTTPReq.responseXML;

var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0){
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert(msg);
}
else{
var results = resultXml.getElementsByTagName('BusinessEntity');
if (results.length != 0)
{
return resultXml.selectNodes("//BusinessEntity/q1:name")[0].text;
}
}
}

No comments:

Split the String values with a special character in MS Flow to convert this into Array

 Many times we have a requirement to prepare the Mailing address for some of the documents, suppose there are Address Line1, Address Line2, ...