Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

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

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
    a33ik 84,323 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
    Summers 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
    a33ik 84,323 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
    Summers 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
    a33ik 84,323 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
    Summers 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
    a33ik 84,323 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
    Summers 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
    a33ik 84,323 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans