web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Check userrole in ms crm javascript Function

(0) ShareShare
ReportReport
Posted on by 1,000

I use the following code for retrieve the role.But it shows the error.


function Validation()
{
alert('Function Entered')
var issubmitted = Xrm.Page.data.entity.attributes.get('new_issubmitted').getValue();
if(UserHasRole("System Administrator"))
{

}

}

function UserHasRole(roleName)
{
var serverUrl = Xrm.Page.context.getClientUrl();

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.length == 1)
{
var role = requestResults[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;
}

Following Error I got

5618.Error.png

*This post is locked for comments

I have the same question (0)
  • Varsha deshpande Profile Picture
    135 on at

    Hi Vijay,

    I tried your code but I haven't got any error. If your requirement is getting user roles try the below code.

    function CheckUserRole() {
    var currentUserRoles = Xrm.Page.context.getUserRoles();
    for (var i = 0; i < currentUserRoles.length; i++) {
    var userRoleId = currentUserRoles[i];
    var userRoleName = GetRoleName(userRoleId);
    if (userRoleName == "System Administrator")
    {
    //action to be performed
    }
    else{
    //action to be performed
    }
    }
    }


    function GetRoleName(roleId) {
    var serverUrl = Xrm.Page.context.getClientUrl();
    var odataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc" + "/" + "RoleSet?$top=1&$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;
    }

    Hope this might be helpful.

  • Suggested answer
    Rushikesh Bhujbal Profile Picture
    285 on at

    I have tried your code., and found only one small issue

    Change

    "if (requestResults != null && requestResults.length == 1)

    {

    var role = requestResults[0];

    "

    to

    "if (requestResults != null && requestResults.results.length == 1) {

               var role = requestResults.results[0]; "

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans