Get formContext inside ribbon event handlers in Dynamics 365

Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com
Before Xrm.Page was deprecated, getting the client URL and access form attributes and controls were so easy in custom ribbon button handlers. A
All I needed to write to get the ClientUrl

var clientUrl = Xrm.Page.context.getClientUrl();

And to get value of the field on the form

var fieldValue = Xrm.Page.getAttribute(“<fieldname>”).getValue()

So what’s the fuss? Well, if your CRM version is 8.2 and below, no worries. It it perfect.
However with version 9.0 and above, the Xrm.Page has been deprecated. So it means you can use them no longer.
So how do I get the Client Url? The below code does it for you.

var globalContext = Xrm.Utility.getGlobalContext();
var clientUrl = globalContext.getClientUrl();

And how about getting the value of a field on the form?
With version 9.0 and above, you should try to access the form data using the formContext.  So the next question lies on how to pass formContext parameter to your Ribbon actions.


For that you need to pass the CrmParameter – Primary control to your ribbon action. Below is the code to get the data using formContext.
image

function ribbonHandler(fc) {
var formContext = fc;
    var recordId = formContext.data.entity.getId();
var fieldValue = formContext.getAttribute("<field_name>").getValue();
}

Just another day as a consultant and I hope this blog just adds a grain to your CRM knowledge heap.
Debajit Dutta
(Dynamics MVP)
For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Comments are closed.