For OnLoad/OnSave/OnChange we are passing the execution context to the methods, but how to pass the execution context for the HTML webresources placed on the CRM forms?
*This post is locked for comments
For OnLoad/OnSave/OnChange we are passing the execution context to the methods, but how to pass the execution context for the HTML webresources placed on the CRM forms?
*This post is locked for comments
have you found the solution ?
Dynamics provides parameters (at least) to Webresource. And to grab that params we need to access to execution context: executionContext.getFormContext().data.attributes;
So your explanation contradicts with webresource options.
In most scenarios, we need execution context in html webresources (for example, an IFrame embbeded on the form) is just for accessing the parent form's attributes, or do direct operation on the parent CRM form (for example, set the CRM form fields with new values, show/hide the CRM form sections or tabs).
If that is the case, the best option is using window.postMessage & event listener to communicate data between the html webresource and form JS, so you do not need to access the exuectioncontext anymore in the html webresource. You can move those logic out from html webresouces to the form JS, and process them in the receive message events.
For example:
Suppose we have a contact form, and html webresource embbeded as the contact_iframe, we have a requirement to change the contact's phone number based on the value on the iframe.
Previously it is using something like this
<html> <script src="../clientGlobalcontext.js.aspx"></script> <script type="text/javascript"> //Set contact form's telephone number with some value on this html //webresource function SetContactFormPhone(newValue){ parent.window.Xrm.Page.getAttribute('telephone1').setValue(newValue); } </script> ..... </html>
Now you should Use window.postMessage in the html webresources, it will communicate between
html webresource and CRM form
<html> <script src="../clientGlobalcontext.js.aspx"></script> <script type="text/javascript"> //Post new telephone number to the parent contact form function SetContactFormPhone(newValue){ var serverUrl = window.parent.Xrm.Utility.getGlobalContext().getClientUrl(); var phonenumber = {}; phonenumber.value = newValue; window.parent.postMessage(JSON.stringify(phonenumber ), serverUrl); } </script> ..... </html>
Then you can change the contact form JS,
add event listener on the parent form, and put the logic into receiveMessage callback function
//Listener is trying to get the message send from Html Webresource function AddListener() { if (window.XMLHttpRequest) { //for browsers other than ie window.parent.addEventListener("message", receiveMessage, false); } else { //ie window.parent.attachEvent('onmessage', receiveMessage); } } function receiveMessage(event) { if(event.origin === serverUrl){ var phonenumber = JSON.parse(event.data) formContext.getAttribute("telephone1").setValue(phonenumber.value) } }
For more details about how to using windows.postMessage and eventListener, please referencing the following article
https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
Hi Jaya,
executionContext is only for form and you will not get the executionContext in your HTML web resource , so there is no way to use executioncontext/formContext in html web resource or we can say Dynamics 365 does not support formcontext inside HTML webresource.
I believe you asked this question as formcontext replaced by Xrm.Page in dynamics 365 , Just to avoid confusion Microsoft says that Xrm.Page will be deprecated but not yet so you can use Xrm.Page in your HTML web resource until official announcement. You can refer below msdn reference -
There is no issue as Microsoft never said not to use Xrm.Page in HTML web resource rather Microsoft says that you can use. You can refer below msdn reference
So In summary use Xrm.Page in HTML web resource and there is no alternate solution provided by Microsoft till now. You should replace Xrm.Page with formContext those JavaScript function are communicating with the OOB form itself.
Normally we will include the below snippet in HTML webresource head section, this will present you the CRM context & controls outside CRM form.
<head>
<title>HTML Web Resource</title>
<script src="../ClientGlobalContext.js.aspx" type="text/javascript" ></script>
</head>
Then access the controls like following:
parent.Xrm.Page.getAttribute("my_control").getValue();
Embedding and passing parameters
To pass context of execution (entity and identifier or a record) or some custom parameters you have to define it in webresource configuration:
To parse and use parameters in your webresource you can use following code:
var parameters = GetGlobalContext().getQueryStringParameters();
Important Note: to make GetGlobalContext method available you have to reference ClientGlobalContext.js.aspx in your code:
<script type="text/javascript" src="../ClientGlobalContext.js.aspx"></script>
Please refer below link..
https://community.dynamics.com/crm/b/hardworkdays/archive/2015/02/28/howto-html-js-webresources
Please mark as verified, thanks in advance !!
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,280 Super User 2024 Season 2
Martin Dráb 230,235 Most Valuable Professional
nmaenpaa 101,156