Get Login User Role name(s) in JavaScript using OData in Dynamics CRM 2011

Here is the sample JavaScript code to get login user role.
Make sure you have added jquery-1.9.1.min.js and JSON2.js in Entity form libraries.If not you can download these from following URL’s
http://jquery.com/download/
http://www.json.org/js.html

//Check login User has 'System Administrator' role
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") {
            return true;
        }
    }
    return false;
}

//Get Rolename based on RoleId
function GetRoleName(roleId) {
    //var serverUrl = Xrm.Page.context.getServerUrl();
    var serverUrl = location.protocol + "//" + location.host + "/" + Xrm.Page.context.getOrgUniqueName();
    var odataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc" + "/" + "RoleSet?$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;
}
Advertisement

Get Account Status in JavaScript using OData in Dynamics CRM 2011

Here is the sample JavaScript code to get account status.
Make sure you have added jquery-1.9.1.min.js and JSON2.js in Entity form libraries. If not you can download these from following URL’s
http://jquery.com/download/
http://www.json.org/js.html

function GetAccountStatus(accountId) {

    //var serverUrl = Xrm.Page.context.getServerUrl();
    var serverUrl = location.protocol + "//" + location.host + "/" + Xrm.Page.context.getOrgUniqueName();

    var odataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc" + "/" + "AccountSet?$filter=AccountId eq guid'" + accountId + "'";

    $.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 accountState = data.d.results[0].StateCode.Value;
            alert(accountState);
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + textStatus + errorThrown + odataSelect); }
    });
}