Tuesday 20 June 2017

{know-how}MSCRM open external url using Xrm.Utility.openWebResource - D365



Hello All,

Good to be back with another know-how post, lets see how to open an external url using Xrm.Utility.OpenWebresource

Scenario:
On click of search ribbon button in account form of CRM, open an external url(bing search) using Xrm.Utility.openWebResource

Why to use Xrm.Utility.openWebResource
Window.open is not a supported method to open a webresource

How?
  1. Open a custom HTML webresource using openWebResource method and pass the encoded external URL as query string parameter
  2. In the webresource, redirect to the encoded URL in  parameter.

Steps and code explanation: 
  1. Create a custom HTML webresource(use the code below).
  2. Onload of page, call a js function named getParameters.
  3. The function will retrieve the encoded URL available in query string
  4. Then it will redirect to the encoded external URL(Bing search)

Code snippets

To open Webresource:
var customParameters = encodeURIComponent("<URL goes here>");
Xrm.Utility.openWebResource("ram_webContainer.html", customParameters)

HTML webresource named ram_webContainer.html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script type="text/javascript" src="ClientGlobalContext.js.aspx"></script>
    <script type="text/javascript">
        var CRMCustomParameters = [];
        var getParameters = function () {
            var Parameters = GetGlobalContext().getQueryStringParameters();
            if (Parameters && Parameters.Data) {
                location.href = Parameters.Data;
            }

        }
       
    </script>
</head>
<body onload="getParameters()">

</body>
</html>

Hope this helps!

5 comments:

  1. This is brilliant and exactly what I needed. Thank you for sharing your knowledge! May your life be bountiful and great :)

    ReplyDelete
  2. Worked like a charm... Thanks a lot

    ReplyDelete
  3. This does not work in UCI

    ReplyDelete
    Replies
    1. https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-navigation/openwebresource

      Using openWebResource is deprecated in Xrm.Utility, try Xrm.Navigation

      Delete
    2. Thanks @chrispytwist. @manish would recomemend to use latest JS object model i.e., Xrm.Navigation.

      Delete