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