1

I am working on Microsoft Dynamics CRM 2016. I have a description field on my Lead form along with that I have added an HTML web resource. When I enter a description value on the CRM form, it should display on my HTML web resource.

How can this be achieved?

HTML web resource code:

<html>
<head>
<script src="../ClientGlobalContext.js.aspx" type="text/javascript"></script>
<script text="text/javascript">
    var description = Xrm.Page.getAttribute("description").getValue();
</script>
</head>
<body>
<table>
  <thead><tr><th>Parameter</th><th>Value</th></tr></thead>
  <tbody>
   <tr><td>description</td><td id="description">null</td></tr>

  </tbody>
 </table>
</body>
</html>

Then in the web resource's properties I have:

  • added a custom parameter: (data)= RelatedEntity=leads&RelatedField=description
  • enabled the option to pass record object-type code and unique identifier as parameters
  • added description in dependencies
Arun Vinoth
  • 20,360
  • 14
  • 48
  • 135
pkm
  • 57
  • 1
  • 8
  • You should expand the question to show you tried to solve the problem (i.e. describe one or more solutions you attempted but don't work for you and specify *why* they don't work for you) otherwise the question is going to be downvoted and ignored because of lack of research on your part. Also, include the CRM version (2016 means 8.0 or 8.1 ?) – Alex May 08 '17 at 11:58

2 Answers2

2

You could attach a JavaScript onChange() event to the description attribute. The event would get the value of the description, then get the element from your HTML control and then set the element's value equal to the description attribute's value.

Here's an example of how it might look:

function descriptionOnChange() {
    // Get the value of the description attribute.
    var description = Xrm.Page.getAttribute('description').getValue();

    // Get the HTML iFrame object.
    var iFrame = Xrm.Page.ui.controls.get('IFRAME_WebResourceName').getObject();

    // Get the element from the iFrame.
    var element = iFrame.contentWindow.document.getElementById('htmlDescription');

    // Set the element's value.
    element.value = description;
}

Note: ensure cross-frame scripting is not disabled in your iFrame's properties:

cross-frame scripting enabled


You may however receive an error if cross-domain iFrame requests are blocked. See this post for an explanation and workaround.

The workaround's implementation in CRM might look like this:

  1. Add a <script> tag to your HTML's <body> to accept and process a message:

    <script>
        window.addEventListener('message', function(event) {
            if (~event.origin.indexOf('https://<yourCRMUrl>')) {
                document.getElementById('htmlDescription').value = event.data;
            } else {
                return;
            }
        })
    </script>
    
  2. Change the contents of your description attribute's onChange() event to this:

    var description = Xrm.Page.getAttribute("description").getValue();
    var iFrame = Xrm.Page.ui.controls.get('IFRAME_WebResourceName').getObject();
    
    iFrame.contentWindow.postMessage(description, '*');
    
Community
  • 1
  • 1
Dave Clark
  • 2,149
  • 13
  • 32
  • or just make the web resource display the value of the field from the WebAPI and in the form refresh the iframe onsave – Alex May 08 '17 at 15:14
  • I've made quite a few assumptions given the limited information in the question. One is that the sooner the HTML iFrame can receive the data the better. – Dave Clark May 08 '17 at 15:48
  • Thank you for reply. I have edited my question which is whatever I have tried. Can you please help me based on that? – pkm May 09 '17 at 04:44
1

You can try something like this:

<html>
<head>
<script src="ClientGlobalContext.js.aspx" type="text/javascript"></script>
<script type="text/javascript">
function onLoad() {
    var description = parent.Xrm.Page.getAttribute("description").getValue();
    document.getElementById("description").innerHTML = description;
}
</script>
</head>
<body onload="onLoad()">
<table>
  <thead><tr><th>Parameter</th><th>Value</th></tr></thead>
  <tbody>
   <tr><td>description</td><td id="description">null</td></tr>
  </tbody>
 </table>
</body>
</html>

Also, as your code grows you can move it into its own web resource, and include it in the html head like this:

 <script src="myLibrary.js" type="text/javascript"></script>
Aron
  • 3,522
  • 3
  • 12
  • 20
  • parent.Xrm.Page.getAttribute() is deprecated. – Dennis Rieke Feb 11 '20 at 10:17
  • 1
    [This article](https://docs.microsoft.com/en-us/power-platform/important-changes-coming) from 02/05/2020 says: _Although Xrm.Page is deprecated, parent.Xrm.Page will continue to work in case of HTML web resources embedded in forms as this is the only way to access the form context from the HTML web resource._ – Aron Feb 11 '20 at 13:53
  • Thank you so much! This article helped me verry much. – Dennis Rieke Feb 12 '20 at 08:29