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 365 | Integration, Dataverse...
Answered

XRrm.Page.Context will be deprecated however Global Context doesn't work for me on prem 8.2

(0) ShareShare
ReportReport
Posted on by 52

On a ribbon button I have the following JS to detect the role of the user. I have been reading that Xrm.Page.Context is deprecated, however if I try updating the script below to use Global Context, it always returns undefined and doesn't work. This is my present code that works (please suggest how to update):


function getrolesarray(formcontextfromribbon) {
    var Globalcontext = Xrm.Page.context;
    var strFirstThree = Globalcontext.getVersion().substring(0, 3);
    var sameunit = true;
    var noclosed_status = true;
    var selview = 0;

    var roleid = Globalcontext.getUserRoles();

    var RoleName_concat = "";

    var user_sysadmin_yesno = false;

    var name;
    for (var i = 0; i < roleid.length; i  ) {
        var roleID = roleid[i];
        var RoleName = getRoleName_XMLHttp(roleID,strFirstThree);

        RoleName = RoleName.toUpperCase()

        if ((RoleName == 'SYSTEM ADMINISTRATOR')) {

            user_sysadmin_yesno = true;
             
            break;
        }

    }

    return user_sysadmin_yesno;


}
function getRoleName_XMLHttp(roleID,strFirstThree) {
    var roleName = null;
    var param = roleID.toString().replace("{", "").replace("}", "");
    var ajaxurl = Globalcontext.getClientUrl()   "/api/data/v"   strFirstThree   "/roles("   param   ")?$select=_businessunitid_value,name,roleid";
    var req = new XMLHttpRequest();
    req.open("GET", ajaxurl, false);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var result = JSON.parse(this.response);
                var _businessunitid_value = result["_businessunitid_value"];
                var _businessunitid_value_formatted = result["_businessunitid_value@OData.Community.Display.V1.FormattedValue"];
                var _businessunitid_value_lookuplogicalname = result["_businessunitid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                var name = result["name"];
                var roleid = result["roleid"];
                roleName = result["name"];
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();

    return roleName;
}


If I try to replace line 2 with:
var Globalcontext = Xrm.Utility.getGlobalContext();
It will be undefined.
I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I would recommend to use roles that are available in the context already instead of querying it - docs.microsoft.com/.../usersettings

  • Summers Profile Picture
    52 on at

    Andrew thanks for the suggestion, however it still doesn't address the issue that getGlobalContext() doesn't work for me.

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    What "doesn't work" for me means? It returns null, it throws an error when you call it?

  • Summers Profile Picture
    52 on at

    it returns undefined and the JS just stops bubbling back. Not sure if because I'm on version 8.2, but it just seems like the JS stops running when I use that line of code.

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    If you're on 8.2 and have no plans to upgrade to 9.0 in foreseeable future - there is no need to update your code.

  • Summers Profile Picture
    52 on at

    We are looking to upgrade in the future (maybe end of year or early next year), but the question still remains... does getGlobalContext() suppose to work in 8.2 or not? It be helpful if someone can verify this, we don't want to continue to use deprecated solutions if not necessary.

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Check it yourself. Open CRM, open developer tools (by clicking F12), choose "Console", enter Xrm.Utility in the console, hit enter, expand the object you get and check if getGlobalContext methos is there.

  • Summers Profile Picture
    52 on at

    Thanks for the excellent suggestion, however that extension did not show:

    0081.Capture.JPG

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    It means that getGlobalContext was introduced in 9.0 and not available in 8.2 so... you would not be able to use it in 8.2 and you will have to wait till migration to 9.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 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 51 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 38 Super User 2025 Season 2

#3
#ManoVerse Profile Picture

#ManoVerse 31

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans