Function returns all the roles of the Current Logged-In User.
function GetCurrentUserRoles()
{
var xml = "" +
"" +
"<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() +
" <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>role</q1:entityname>" +
" <q1:columnset xsi:type="\"Q1:ColumnSet\"">" +
" <q1:attributes>" +
" <q1:attribute>name</q1:attribute>" +
" </q1:attributes>" +
" </q1:columnset>" +
" <q1:distinct>false</q1:distinct>" +
" <q1:linkentities>" +
" <q1:linkentity>" +
" <q1:linkfromattributename>roleid</q1:linkfromattributename>" +
" <q1:linkfromentityname>role</q1:linkfromentityname>" +
" <q1:linktoentityname>systemuserroles</q1:linktoentityname>" +
" <q1:linktoattributename>roleid</q1:linktoattributename>" +
" <q1:joinoperator>Inner</q1:joinoperator>" +
" <q1:linkentities>" +
" <q1:linkentity>" +
" <q1:linkfromattributename>systemuserid</q1:linkfromattributename>" +
" <q1:linkfromentityname>systemuserroles</q1:linkfromentityname>" +
" <q1:linktoentityname>systemuser</q1:linktoentityname>" +
" <q1:linktoattributename>systemuserid</q1:linktoattributename>" +
" <q1:joinoperator>Inner</q1:joinoperator>" +
" <q1:linkcriteria>" +
" <q1:filteroperator>And</q1:filteroperator>" +
" <q1:conditions>" +
" <q1:condition>" +
" <q1:attributename>systemuserid</q1:attributename>" +
" <q1:operator>EqualUserId</q1:operator>" +
" </q1:condition>" +
" </q1:conditions>" +
" </q1:linkcriteria>" +
" </q1:linkentity>" +
" </q1:linkentities>" +
" </q1:linkentity>" +
" </q1:linkentities>" +
" </query>" +
" </retrievemultiple>" +
" </soap:body>" +
"</soap:envelope>" +
"";
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction"," http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseXML;
return(resultXml);
}
Function which calls the above function and search for indivisual role:
function UserHasRole(roleName)
{
//get Current User Roles, oXml is an object
var oXml = GetCurrentUserRoles();
if(oXml != null)
{
//select the node text
var roles = oXml.selectNodes("//BusinessEntity/q1:name");
if(roles != null)
{
for( i = 0; i < roles.length; i++)
{
if(roles[i].text == roleName)
{
//return true if user has this role
return true;
}
}
}
}
//otherwise return false
return false;
}
How to use UserHasRole function:
if(UserHasRole(“System Administrator”) == true)
{
alert(‘User has admin role’);
}
else
{
alert(‘User does not have admin role’);
}