Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Xrm.WebApi method in Web Resource opened in a new window

I have opened an HTML web resource in a new window using:

Xrm.Navigation.openWebResource(webResource, windowOptions, data);

This is an HTML web resource and it is loading the ClientObject in the head

<script type="text/javascript" src="../../../ClientGlobalContext.js.aspx" ></script>

then I have some JavaScript that is trying to retrieve a Contact

var contactId = "8553DA63-11C9-E711-A824-000D3AE0CB84";
var promise = Xrm.WebApi.retrieveRecord("contact", contactId, "$select=contactid,firstname,lastname");`

but this is failing. I've step-traced into the Xrm.WebApi method and found the error is when it attempts to resolve "contact" to a Set Name

Code from Global.ashx

getEntitySetName: function(logicalName) {
    Mscrm.Utilities.addTelemetryLog("Xrm.Utility.getEntitySetName");
    var $v_0 = window.ENTITY_SET_NAMES || window.top.ENTITY_SET_NAMES;
    if (IsNull(this.$5H_1) && !isNullOrEmptyString($v_0))
        this.$5H_1 = JSON.parse($v_0);
    return this.$5H_1[logicalName.toLowerCase()]
},

For some reason the window.ENTITY_SET_NAMES object is null so an error (null reference) occurs

I've tried embedding my web resource into a CRM page and the code works correctly. The issue seems to be when the web resource is launched via Xrm.Navigation.openWebResource

Has anyone tried to use Xrm.WebApi in the context of an web resource opened with Xrm.Navigation.openWebResource? or does anyone know if there are additional steps required to retrieve data?


Update

ENTITY_SET_NAMES is initialised in main.aspx. I tried embedding my custom Web Resource directly into a new Main Form section and the retrieveRecord method works.

It appears this is a problem only when running the Web Resource from a new page via Xrm.Navigation.openWebResource


Update 2 - Response to Aron

I tried using window.parent as suggested below

var contactId = "8553DA63-11C9-E711-A824-000D3AE0CB84";
var promise = parent.Xrm.WebApi.retrieveRecord("contact", contactId, "$select=contactid,firstname,lastname");`

and for good measure also tried window.parent.top

var contactId = "8553DA63-11C9-E711-A824-000D3AE0CB84";
var promise = parent.top.Xrm.WebApi.retrieveRecord("contact", contactId, "$select=contactid,firstname,lastname");`

but both resulted in the same error

like image 369
jasonscript Avatar asked Feb 03 '23 22:02

jasonscript


2 Answers

Sounds like a product bug within ClientGlobalContext.js.aspx, as this should give you whole context to work with.

Probably you can utilize window.opener.Xrm in this scenario, since it worked for window.opener.Xrm.Page.getAttribute it should also work for Xrm.WebApi.

You can try to access variable from opener window like this:

window["ENTITY_SET_NAMES"] = window["ENTITY_SET_NAMES"] || window.opener.top.ENTITY_SET_NAMES;
like image 181
Arun Vinoth - MVP Avatar answered Feb 06 '23 16:02

Arun Vinoth - MVP


My blog :)


To get this working I have implemented a hacky work-around.

I've been debugging the Xrm.WebApi method and it is failing on a line where it attempts to take the entityname and resolve it to the setname (plural). It does this by comparing the value passed into the retrieveRecord method and comparing it to a global variable ENTITY_SET_NAMES

In my example, it is trying to resolve contact to contacts

This variable is unfortunately not present and Xrm.WebApi throws an error

My work-around is to check for this variable, and if it is not present then create it! ENTITY_SET_NAMES is a JSON-parsable string which contains the logical name and set name for each entity.

window["ENTITY_SET_NAMES"] = window["ENTITY_SET_NAMES"] || JSON.stringify({
  "account" : "accounts",
  "contact" : "contacts"
});

Executing this line before any calls to Xrm.WebApi methods appears to work and I now get results

Here's the complete snippet:

window["ENTITY_SET_NAMES"] = window["ENTITY_SET_NAMES"] || JSON.stringify({
  "account" : "accounts",
  "contact" : "contacts"
});

var contactId = "8553DA63-11C9-E711-A824-000D3AE0CB84";
Xrm.WebApi.retrieveRecord(
  "contact", 
  contactId, 
  "$select=contactid,firstname,lastname"
).then(
  function success(result) {
    console.log(result.firstname);
    // perform operations on record retrieval
  },
  function (error) {
    console.log(error.message);
    // handle error conditions
  }
);
like image 33
jasonscript Avatar answered Feb 06 '23 16:02

jasonscript