With the release of version 9.0 of Dynamics CRM/365, Microsoft announced that it would deprecate part of the JavaScript client API. Part of the client API being deprecated is Xrm.Page. To give enough transition time to the users of Microsoft Dynamics 365, Xrm.Page will continue to function in this transition period, until Microsoft fully retires the client API.
Despite, the Xrm.Page client API deprecation announcement was made 2 years ago, the use of this API continues to be extensive (visit any Dynamics 365 online forums e.g community.dynamics.com). In my line of work, when I ask developers why they are still using a client API destined for deprecation, the common response is that they have no time to learn the new API and the pressing demands of their work, they have more work than the time available. Therefore, I have written this post to help Microsoft Dynamics 365 software developers, who are pressed for time, and need a quick guide to transition away from Xrm.Page. I have provided examples of the most commonly used Dynamics 365 Xrm.Page JavaScript client API functions, on entity forms, and their formContext client API equivalents.
Examples and application
To view the table below, in full, please use a device larger than your mobile phone.
Objective | Using Xrm.Page (Deprecated Client API) | Using formContext (Replacement Client API) |
Simple JavaScript function | function demoFunction(){ } | function demoFunction(eContext){ |
Disable/lock field, “addRegions”, for selection | Xrm.Page. getControl("addRegions"). setDisabled(true); | formContext. getControl("addRegions"). setDisabled(true); |
Enable/unlock field, “addRegions”, for selection | Xrm.Page. getControl("addRegions"). setDisabled(false); | formContext. getControl("addRegions"). setDisabled(false); |
Hide field, “addRegions” | Xrm.Page. getControl("addRegions"). setVisible(false); | formContext. getControl("addRegions"). setVisible(false); |
Show field, “addRegions” | Xrm.Page. getControl("addRegions"). setVisible(true); | formContext. getControl("addRegions"). setVisible(true); |
Get lookup field “region”, and its properties | var regionLookup = Xrm.Page. getAttribute("region").getValue(); id.slice(1, -1); regionLookup[0] .entityType; | var regionLookup = formContext. getAttribute("region").getValue(); id.slice(1, -1); regionLookup[0].entityType; |
Remove option, 15220000, from ‘showRegions’ option set. | Xrm.Page. getControl("showRegions"). removeOption(15220000); | formContext. getControl("showRegions"). removeOption(15220000); |
Add option, 15220001 (with text: ‘Africa’), to ‘showRegions’ option set. | Xrm.Page. getControl("showRegions"). addOption({ | formContext. getControl("showRegions"). addOption({ |
Show tab, with logical name “regionsTab” | Xrm.Page.ui.tabs. get("regionsTab"). setVisible(true); | formContext.ui.tabs. get("regionsTab"). setVisible(true); |
Hide tab, with logical name “regionsTab” | Xrm.Page.ui.tabs. get("regionsTab"). setVisible(false); | formContext.ui.tabs. get("regionsTab"). setVisible(false); |
Show section “rgsTab_rgSec”, located in tab “regionsTab” | Xrm.Page.ui.tabs. get("regionsTab"). | formContext.ui.tabs. get("regionsTab"). sections.get("rgsTab_rgSec"). setVisible(true); |
Hide section “rgsTab_rgSec”, located in tab “regionsTab” | Xrm.Page.ui.tabs. get("regionsTab").sections. get("rgsTab_rgSec"). | formContext.ui.tabs. get("regionsTab").sections. get("rgsTab_rgSec"). setVisible(false); |
Count number of records on sub-grid called “demo-grid” | var count = Xrm.Page. getTotalRecordCount(); | var count = formContext. getControl("demo-grid") .getGrid(). getTotalRecordCount(); |
Transition from deprecated client API table
To view the table below, in full, please use a device larger than your mobile phone.
Conclusion
Time saved now is time saved in the future. Therefore, I am of the view that it is better to take the time to learn the new Dynamics 365 JavaScript client API and begin to write your code using an API that will continue to function beyond the short/medium term. If today, a team begins to use the new client API to write new code, they will have less legacy code to update when Microsoft pulls the plug on Xrm.Page and other deprecated APIs. Even better, a team can learn the new JavaScript client API, update their legacy code and stop using the deprecated client API. Taking the latter approach, a team will not need to perform an emergency update of their legacy code when Microsoft eventually completely retires the Xrm.Page and other deprecated APIs.