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.
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: XRrm.Page.Context will be deprecated however Global Context doesn't work for me on prem 8.2

    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.

  • Summers Profile Picture
    52 on at
    RE: XRrm.Page.Context will be deprecated however Global Context doesn't work for me on prem 8.2

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

    0081.Capture.JPG

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: XRrm.Page.Context will be deprecated however Global Context doesn't work for me on prem 8.2

    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
    RE: XRrm.Page.Context will be deprecated however Global Context doesn't work for me on prem 8.2

    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
    RE: XRrm.Page.Context will be deprecated however Global Context doesn't work for me on prem 8.2

    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
    RE: XRrm.Page.Context will be deprecated however Global Context doesn't work for me on prem 8.2

    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
    RE: XRrm.Page.Context will be deprecated however Global Context doesn't work for me on prem 8.2

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

  • Summers Profile Picture
    52 on at
    RE: XRrm.Page.Context will be deprecated however Global Context doesn't work for me on prem 8.2

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

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: XRrm.Page.Context will be deprecated however Global Context doesn't work for me on prem 8.2

    Hello,

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

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 149 Super User 2025 Season 1

#2
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 53 Super User 2025 Season 1

#3
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 32 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans