locked
CRM2011 - Ribbon javascript function requires another javascript web resource dependency. RRS feed

  • Question

  • Perhaps I don't understand the management of the web resources correctly, but what I am trying to do is call a javascript function from a ribbon button (which is easy enough) but in that script, I need to make a call to another javascript library which has some useful functions.

    How will I know if other javascript files have been loaded when that ribbon button is clicked? Am I missing an obvious/easy way to do this?

    I am thinking if there isn't a CRM standard way, then perhaps I need to manually load the javscript library using the web resource URL. (Perhaps only if it isn't already loaded)

    Any thoughts/comments would be helpful.

    Thursday, April 21, 2011 3:53 AM

Answers

All replies

  • Why can't you have that library to set to onload of the form or even onreadystate of the subgrid? 
    • Edited by PurvinP Thursday, April 21, 2011 8:49 AM clarity
    Thursday, April 21, 2011 8:49 AM
  • I have the same problem, and my js is executed to populate a dynamic menu in the jewel, so there is no form or grid event to be triggered by.

    Please advise :)

    /Jonas

    Thursday, April 21, 2011 9:07 AM
  • You can check if the function you're trying to call already exists like this:

    if(typeof yourFunctionName == 'function') {
    yourFunctionName();
    }

    Thursday, April 21, 2011 1:35 PM
  • Hi,

    You can doing it using following code:

    if( functionName ) {
      functionName();
    }
    

    Thursday, April 21, 2011 1:52 PM
  • If there is no CRM provided way to have these scripts loaded, does anyone see any issue with dynamically loading the scripts using the web source url?

    If not, does anyone know a clean bit of js to dynamically load a js web resource using the wr url?

    Friday, April 22, 2011 12:25 AM
  • Hi

    I use this little function to load extra js webresources.

    Note that this only works for onprem installations, as I have hard coded where the orgname should be inserted.

    function LoadWebResource(resource) {
      var httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
      httpRequest.open("GET", "/" + _orgname + "/webresources/" + resource, false);
      httpRequest.send(null);
      try {
        eval(httpRequest.responseText);
      }
      catch (e) {
        alert("Error loading " + resource + ":\n" + e.description);
      }
    }
    

    My ribbon command action looks like this:

        <Actions>
         <JavaScriptFunction FunctionName="DynamicMenu" Library="$webresource:new_/myscript.js">
          <CrmParameter Value="CommandProperties" />
          <CrmParameter Value="OrgName" />
         </JavaScriptFunction>
        </Actions>
    

    And the function referenced from the action:

    var _orgname;
    
    function DynamicMenu(CommandProperties, OrgName) {
      _orgname = OrgName;
      try {
        LoadWebResource("new_/scripts/js_library.js");
        ...
      }
      catch (e) {
        alert("Error populating submenu:\n" + e.description);
        throw (e);
      }
    }
    
    /Jonas

    Friday, April 22, 2011 10:32 AM
  • Thanks for the sample.

    I suspect that OrgName could be replaced with the global constant ORG_UNIQUE_NAME as per http://msdn.microsoft.com/en-us/library/cc150876.aspx

    function LoadWebResource(resource) {
     var httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
     httpRequest.open("GET", "/" + ORG_UNIQUE_NAME + "/webresources/" + resource, false);
     httpRequest.send(null);
     try {
      eval(httpRequest.responseText);
     }
     catch (e) {
      alert("Error loading " + resource + ":\n" + e.description);
     }
    }
    
    Monday, May 2, 2011 4:14 AM
  • Hi Glenn

    Actually, I'm not sure you can.

    As we are not in a form or specific grid here, and have not loaded the ClientGlobalContext.js.aspx - are there any global constants available...?

    /Jonas


    Monday, May 2, 2011 6:59 AM
  • A smiliar question was asked on stack-overflow. I commented this particular scenario and got a response (read the comments on the bottom answer)

    http://stackoverflow.com/questions/4994083/how-do-you-have-multiple-javascript-libraries-in-dynamics-crm-2011

     

    "In this instance, you can add all the libraries to the Ribbon XML. For each library, add a <JavaScriptFunction Library="$webresource:webresourceName" FunctionName="isNaN" /> node. It requires a function name, so we've just been putting isNaN in there so there's no error." – Matt Apr 21 at 12:28

     

    Hope this helps someone, like it helped me :)



    Wednesday, May 11, 2011 2:40 AM
  • Luke and Mike are on the best track. Just add a bogus command defintion with at JavaScriptFunction action with the library you need. When CRM processes the ribbon the library will be loaded in that context. I am hoping that we can make this eaiser in the future.

    Rich Dickinson
    Dynamics CRM - PM

    Thursday, May 12, 2011 8:45 AM
  • thanks for the hint!!

     

    Monday, June 27, 2011 2:19 PM
  • When I do this, I get a JavaScript error followed by the following code in the error report.

    It works fine on the development server, but when it is moved to production the JavaScript error happens 100% of the time on page load.

    Anyone know what the problem could be?

    It is clearly the isNaN function that causes it when its moved to production.  See below.

    Microsoft Dynamics CRM Error Report Contents

    <CrmScriptErrorReport>
      <ReportVersion>1.0</ReportVersion>
      <ScriptErrorDetails>
       <Message>Syntax error</Message>
       <Line>1</Line>
       <URL></URL>
       <PageURL>/userdefined/edit.aspx?etc=10018&pagemode=iframe&preloadcache=1447693061714</PageURL>
       <Function></Function>
       <CallStack>
       </CallStack>
      </ScriptErrorDetails>
      <ClientInformation>
       <BrowserUserAgent>Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)</BrowserUserAgent>
       <BrowserLanguage>en-US</BrowserLanguage>
       <SystemLanguage>en-US</SystemLanguage>
       <UserLanguage>en-US</UserLanguage>
       <ScreenResolution>1821x1024</ScreenResolution>
       <ClientName>Web</ClientName>
       <ClientTime>2015-11-16T11:57:42</ClientTime>
      </ClientInformation>
      <ServerInformation>
        <OrgLanguage>1033</OrgLanguage>
        <OrgCulture>1033</OrgCulture>
        <UserLanguage>1033</UserLanguage>
        <UserCulture>1033</UserCulture>
        <OrgID>{91BAFA3F-150E-E511-9261-005056B96207}</OrgID>
        <UserID>{4A44E95D-1F0E-E511-9261-005056B96207}</UserID>
        <CRMVersion>5.0.9690.4376</CRMVersion>
      </ServerInformation>
    </CrmScriptErrorReport>

    • Edited by ACECORP Monday, November 16, 2015 5:08 PM
    Monday, November 16, 2015 5:01 PM