Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 55831

CRM OData query - CRM on-premise vs CRM online

$
0
0

Over the weekend I spent time making comparison between CRM Online and On-premise. I the process I picked up some funnies when it comes to JQuery and CRM 2015 Online. I had the libraries an all that on the solution.

 Scenario

I wanted to check if the current user in “System Administrator” using Jscript. The code below was happy on premise but failed online (here : $.ajax). See full code below 

function CheckUserRole(rolename) {

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

    for (var i = 0; i < currentUserRoles.length; i++) {

         var userRoleId = currentUserRoles[i];

    var userRoleName = GetRoleName(userRoleId);

        if (userRoleName == "rolename") {

            return true;

        }

    }

    return false;

}

 

function GetRoleName(roleId) {   

    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;

}

Solution

 

The code below did the job

 

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.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;

}

Credit to :

https://msdynamicscrmblog.wordpress.com/2013/03/10/get-login-user-role-names-in-javascript-in-dynamics-crm-2011/

http://www.infinite-x.net/2010/11/16/retreiving-user-roles-in-crm-2011/

 

 


Viewing all articles
Browse latest Browse all 55831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>