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











up vote
6
down vote

favorite












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










share|improve this question




























    up vote
    6
    down vote

    favorite












    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










    share|improve this question


























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      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










      share|improve this question















      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







      javascript dynamics-crm microsoft-dynamics dynamics-365 dynamics-crm-webapi






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 20 at 1:49

























      asked Jul 19 at 7:13









      jasonscript

      3,51611630




      3,51611630
























          4 Answers
          4






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          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
          }
          );





          share|improve this answer






























            up vote
            2
            down vote













            As per this article, when referencing the main form from a Web Resource we have to reference the parent window. Though, it only references Xrm.Page and Xrm.Utility, it should also work with Xrm.WebApi...




            An HTML web resource added to a form can’t use global objects defined by the JavaScript library loaded in the form. An HTML web resource may interact with the Xrm.Page or Xrm.Utility objects within the form by using parent.Xrm.Page or parent.Xrm.Utility, but global objects defined by form scripts won’t be accessible using the parent.




            Please try parent.Xrm.WebApi.retrieveRecord("contact", contactId, "$select=contactid,firstname,lastname");



            This article also demonstrates parent.Xrm.WebApi






            share|improve this answer























            • great find Aron. Unfortunately it doesn't work. I've updated my question with my test results
              – jasonscript
              Jul 20 at 1:50






            • 1




              Thanks for the update. Since the code works when embedded in the form but fails when opened in a new window, it seems that opening the new window creates a new window object, which breaks window.ENTITY_SET_NAMES. Perhaps you can retrieve it from the parent window and set it on the child window to enable Xrm.WebApi. Or, when working in a new window, perhaps "roll your own" Web API calls. For that purpose Jason Lattimer's CRMRESTBuilder is very handy.
              – Aron
              Jul 20 at 12:48










            • I couldn't retrieve it from the parent window - it was null. The solution I eventually settled on was to create my own ENTITY_SET_NAME global variable with only the values I need. I've added my own answer if you want to see more detail
              – jasonscript
              Jul 20 at 15:27


















            up vote
            2
            down vote













            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;





            share|improve this answer



















            • 1




              I have to specify top but it works: window.opener.top.ENTITY_SET_NAMES
              – jasonscript
              Jul 26 at 1:48


















            up vote
            1
            down vote













            If you are going to use bound actions & functions you'll also need to add a similar variable to map entities to their primary id fields.






            window["ENTITY_PRIMARY_KEYS"] = ['{"account":"accountid", "contact":"contactid"}'];








            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51416490%2fusing-xrm-webapi-method-in-web-resource-opened-in-a-new-window%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              3
              down vote



              accepted










              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
              }
              );





              share|improve this answer



























                up vote
                3
                down vote



                accepted










                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
                }
                );





                share|improve this answer

























                  up vote
                  3
                  down vote



                  accepted







                  up vote
                  3
                  down vote



                  accepted






                  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
                  }
                  );





                  share|improve this answer














                  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
                  }
                  );






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jul 25 at 16:05

























                  answered Jul 20 at 2:05









                  jasonscript

                  3,51611630




                  3,51611630
























                      up vote
                      2
                      down vote













                      As per this article, when referencing the main form from a Web Resource we have to reference the parent window. Though, it only references Xrm.Page and Xrm.Utility, it should also work with Xrm.WebApi...




                      An HTML web resource added to a form can’t use global objects defined by the JavaScript library loaded in the form. An HTML web resource may interact with the Xrm.Page or Xrm.Utility objects within the form by using parent.Xrm.Page or parent.Xrm.Utility, but global objects defined by form scripts won’t be accessible using the parent.




                      Please try parent.Xrm.WebApi.retrieveRecord("contact", contactId, "$select=contactid,firstname,lastname");



                      This article also demonstrates parent.Xrm.WebApi






                      share|improve this answer























                      • great find Aron. Unfortunately it doesn't work. I've updated my question with my test results
                        – jasonscript
                        Jul 20 at 1:50






                      • 1




                        Thanks for the update. Since the code works when embedded in the form but fails when opened in a new window, it seems that opening the new window creates a new window object, which breaks window.ENTITY_SET_NAMES. Perhaps you can retrieve it from the parent window and set it on the child window to enable Xrm.WebApi. Or, when working in a new window, perhaps "roll your own" Web API calls. For that purpose Jason Lattimer's CRMRESTBuilder is very handy.
                        – Aron
                        Jul 20 at 12:48










                      • I couldn't retrieve it from the parent window - it was null. The solution I eventually settled on was to create my own ENTITY_SET_NAME global variable with only the values I need. I've added my own answer if you want to see more detail
                        – jasonscript
                        Jul 20 at 15:27















                      up vote
                      2
                      down vote













                      As per this article, when referencing the main form from a Web Resource we have to reference the parent window. Though, it only references Xrm.Page and Xrm.Utility, it should also work with Xrm.WebApi...




                      An HTML web resource added to a form can’t use global objects defined by the JavaScript library loaded in the form. An HTML web resource may interact with the Xrm.Page or Xrm.Utility objects within the form by using parent.Xrm.Page or parent.Xrm.Utility, but global objects defined by form scripts won’t be accessible using the parent.




                      Please try parent.Xrm.WebApi.retrieveRecord("contact", contactId, "$select=contactid,firstname,lastname");



                      This article also demonstrates parent.Xrm.WebApi






                      share|improve this answer























                      • great find Aron. Unfortunately it doesn't work. I've updated my question with my test results
                        – jasonscript
                        Jul 20 at 1:50






                      • 1




                        Thanks for the update. Since the code works when embedded in the form but fails when opened in a new window, it seems that opening the new window creates a new window object, which breaks window.ENTITY_SET_NAMES. Perhaps you can retrieve it from the parent window and set it on the child window to enable Xrm.WebApi. Or, when working in a new window, perhaps "roll your own" Web API calls. For that purpose Jason Lattimer's CRMRESTBuilder is very handy.
                        – Aron
                        Jul 20 at 12:48










                      • I couldn't retrieve it from the parent window - it was null. The solution I eventually settled on was to create my own ENTITY_SET_NAME global variable with only the values I need. I've added my own answer if you want to see more detail
                        – jasonscript
                        Jul 20 at 15:27













                      up vote
                      2
                      down vote










                      up vote
                      2
                      down vote









                      As per this article, when referencing the main form from a Web Resource we have to reference the parent window. Though, it only references Xrm.Page and Xrm.Utility, it should also work with Xrm.WebApi...




                      An HTML web resource added to a form can’t use global objects defined by the JavaScript library loaded in the form. An HTML web resource may interact with the Xrm.Page or Xrm.Utility objects within the form by using parent.Xrm.Page or parent.Xrm.Utility, but global objects defined by form scripts won’t be accessible using the parent.




                      Please try parent.Xrm.WebApi.retrieveRecord("contact", contactId, "$select=contactid,firstname,lastname");



                      This article also demonstrates parent.Xrm.WebApi






                      share|improve this answer














                      As per this article, when referencing the main form from a Web Resource we have to reference the parent window. Though, it only references Xrm.Page and Xrm.Utility, it should also work with Xrm.WebApi...




                      An HTML web resource added to a form can’t use global objects defined by the JavaScript library loaded in the form. An HTML web resource may interact with the Xrm.Page or Xrm.Utility objects within the form by using parent.Xrm.Page or parent.Xrm.Utility, but global objects defined by form scripts won’t be accessible using the parent.




                      Please try parent.Xrm.WebApi.retrieveRecord("contact", contactId, "$select=contactid,firstname,lastname");



                      This article also demonstrates parent.Xrm.WebApi







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jul 19 at 11:25

























                      answered Jul 19 at 11:07









                      Aron

                      1,8872917




                      1,8872917












                      • great find Aron. Unfortunately it doesn't work. I've updated my question with my test results
                        – jasonscript
                        Jul 20 at 1:50






                      • 1




                        Thanks for the update. Since the code works when embedded in the form but fails when opened in a new window, it seems that opening the new window creates a new window object, which breaks window.ENTITY_SET_NAMES. Perhaps you can retrieve it from the parent window and set it on the child window to enable Xrm.WebApi. Or, when working in a new window, perhaps "roll your own" Web API calls. For that purpose Jason Lattimer's CRMRESTBuilder is very handy.
                        – Aron
                        Jul 20 at 12:48










                      • I couldn't retrieve it from the parent window - it was null. The solution I eventually settled on was to create my own ENTITY_SET_NAME global variable with only the values I need. I've added my own answer if you want to see more detail
                        – jasonscript
                        Jul 20 at 15:27


















                      • great find Aron. Unfortunately it doesn't work. I've updated my question with my test results
                        – jasonscript
                        Jul 20 at 1:50






                      • 1




                        Thanks for the update. Since the code works when embedded in the form but fails when opened in a new window, it seems that opening the new window creates a new window object, which breaks window.ENTITY_SET_NAMES. Perhaps you can retrieve it from the parent window and set it on the child window to enable Xrm.WebApi. Or, when working in a new window, perhaps "roll your own" Web API calls. For that purpose Jason Lattimer's CRMRESTBuilder is very handy.
                        – Aron
                        Jul 20 at 12:48










                      • I couldn't retrieve it from the parent window - it was null. The solution I eventually settled on was to create my own ENTITY_SET_NAME global variable with only the values I need. I've added my own answer if you want to see more detail
                        – jasonscript
                        Jul 20 at 15:27
















                      great find Aron. Unfortunately it doesn't work. I've updated my question with my test results
                      – jasonscript
                      Jul 20 at 1:50




                      great find Aron. Unfortunately it doesn't work. I've updated my question with my test results
                      – jasonscript
                      Jul 20 at 1:50




                      1




                      1




                      Thanks for the update. Since the code works when embedded in the form but fails when opened in a new window, it seems that opening the new window creates a new window object, which breaks window.ENTITY_SET_NAMES. Perhaps you can retrieve it from the parent window and set it on the child window to enable Xrm.WebApi. Or, when working in a new window, perhaps "roll your own" Web API calls. For that purpose Jason Lattimer's CRMRESTBuilder is very handy.
                      – Aron
                      Jul 20 at 12:48




                      Thanks for the update. Since the code works when embedded in the form but fails when opened in a new window, it seems that opening the new window creates a new window object, which breaks window.ENTITY_SET_NAMES. Perhaps you can retrieve it from the parent window and set it on the child window to enable Xrm.WebApi. Or, when working in a new window, perhaps "roll your own" Web API calls. For that purpose Jason Lattimer's CRMRESTBuilder is very handy.
                      – Aron
                      Jul 20 at 12:48












                      I couldn't retrieve it from the parent window - it was null. The solution I eventually settled on was to create my own ENTITY_SET_NAME global variable with only the values I need. I've added my own answer if you want to see more detail
                      – jasonscript
                      Jul 20 at 15:27




                      I couldn't retrieve it from the parent window - it was null. The solution I eventually settled on was to create my own ENTITY_SET_NAME global variable with only the values I need. I've added my own answer if you want to see more detail
                      – jasonscript
                      Jul 20 at 15:27










                      up vote
                      2
                      down vote













                      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;





                      share|improve this answer



















                      • 1




                        I have to specify top but it works: window.opener.top.ENTITY_SET_NAMES
                        – jasonscript
                        Jul 26 at 1:48















                      up vote
                      2
                      down vote













                      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;





                      share|improve this answer



















                      • 1




                        I have to specify top but it works: window.opener.top.ENTITY_SET_NAMES
                        – jasonscript
                        Jul 26 at 1:48













                      up vote
                      2
                      down vote










                      up vote
                      2
                      down vote









                      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;





                      share|improve this answer














                      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;






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jul 26 at 2:13

























                      answered Jul 25 at 18:45









                      Arun Vinoth

                      8,833132446




                      8,833132446








                      • 1




                        I have to specify top but it works: window.opener.top.ENTITY_SET_NAMES
                        – jasonscript
                        Jul 26 at 1:48














                      • 1




                        I have to specify top but it works: window.opener.top.ENTITY_SET_NAMES
                        – jasonscript
                        Jul 26 at 1:48








                      1




                      1




                      I have to specify top but it works: window.opener.top.ENTITY_SET_NAMES
                      – jasonscript
                      Jul 26 at 1:48




                      I have to specify top but it works: window.opener.top.ENTITY_SET_NAMES
                      – jasonscript
                      Jul 26 at 1:48










                      up vote
                      1
                      down vote













                      If you are going to use bound actions & functions you'll also need to add a similar variable to map entities to their primary id fields.






                      window["ENTITY_PRIMARY_KEYS"] = ['{"account":"accountid", "contact":"contactid"}'];








                      share|improve this answer

























                        up vote
                        1
                        down vote













                        If you are going to use bound actions & functions you'll also need to add a similar variable to map entities to their primary id fields.






                        window["ENTITY_PRIMARY_KEYS"] = ['{"account":"accountid", "contact":"contactid"}'];








                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          If you are going to use bound actions & functions you'll also need to add a similar variable to map entities to their primary id fields.






                          window["ENTITY_PRIMARY_KEYS"] = ['{"account":"accountid", "contact":"contactid"}'];








                          share|improve this answer












                          If you are going to use bound actions & functions you'll also need to add a similar variable to map entities to their primary id fields.






                          window["ENTITY_PRIMARY_KEYS"] = ['{"account":"accountid", "contact":"contactid"}'];








                          window["ENTITY_PRIMARY_KEYS"] = ['{"account":"accountid", "contact":"contactid"}'];





                          window["ENTITY_PRIMARY_KEYS"] = ['{"account":"accountid", "contact":"contactid"}'];






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 12 at 4:30









                          Jason Lattimer

                          2,7431014




                          2,7431014






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51416490%2fusing-xrm-webapi-method-in-web-resource-opened-in-a-new-window%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Xamarin.iOS Cant Deploy on Iphone

                              Glorious Revolution

                              Dulmage-Mendelsohn matrix decomposition in Python