简体   繁体   中英

how to get value from microsoft dynamics crm form to html web resource

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

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:

跨框架脚本启用


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, '*'); 

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM