web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to pass the execution context to CRM HTML Webresource in Dynamics 365 V9?

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    MS CRM DYNAMICS Profile Picture
    592 on at
    RE: How to pass the execution context to CRM HTML Webresource in Dynamics 365 V9?

    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:

    6087.1.png

    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 !!

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: How to pass the execution context to CRM HTML Webresource in Dynamics 365 V9?

    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 -

    webres2.jpg

    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 

    https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/webpage-html-web-resources

    webres2.jpg

    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.

  • HarryZang Profile Picture
    240 on at
    RE: How to pass the execution context to CRM HTML Webresource in Dynamics 365 V9?

    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

  • kmish Profile Picture
    50 on at
    RE: How to pass the execution context to CRM HTML Webresource in Dynamics 365 V9?

    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.

  • Community Member Profile Picture
    on at
    RE: How to pass the execution context to CRM HTML Webresource in Dynamics 365 V9?

    have you found the solution ?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans