Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

Posted on by Microsoft Employee

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

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

    have you found the solution ?

  • kmish Profile Picture
    kmish 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.

  • HarryZang Profile Picture
    HarryZang 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

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 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.

  • Suggested answer
    MS CRM DYNAMICS Profile Picture
    MS CRM DYNAMICS 580 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 !!

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

Featured topics

Product updates

Dynamics 365 release plans