1

I have a script that is launched in ribbon command in order to perform an action but it's written in simple javascript. As I would like to improve it, I would like to use JQuery instead. The script is located in a solution that is uploaded in solutions, should I add the jquery file inside the solution ? How can I use it ?

Hubert Solecki
  • 2,611
  • 5
  • 31
  • 63
  • What context is the ribbon button being ran in? Is it on the form or while on a view? The answer will vary depending on your response. – hack3rfx Nov 23 '16 at 16:07
  • 1
    https://msdn.microsoft.com/en-us/library/hh771584.aspx#BKMK_UsingjQuery > **Avoid using jQuery with form scripts or ribbon commands** > We do not recommend using jQuery in form scripts and ribbon commands. > Most of the benefit provided by jQuery is that it allows for easy > cross-browser manipulation of the DOM. This is explicitly unsupported > within form scripts and ribbon commands. Restrict your scripts to use > the Xrm.Page and Xrm.Utility libraries available in form scripts and > ribbon commands. – mktange Nov 23 '16 at 09:54
  • 1
    Thank you but it doesn't answer to my question.... – Hubert Solecki Nov 23 '16 at 09:56
  • Then my question will be: Why do you need to use JQuery? – mktange Nov 23 '16 at 09:58

4 Answers4

5

You should be able to access jQuery like this:

$ = ($ || parent.$);
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Alex
  • 23,004
  • 4
  • 39
  • 73
  • 1
    To clarify: CRM is made with jQuery so there is no need to include it again if it's needed by JS web resources, it's already there and can be used – Alex Nov 24 '16 at 15:16
3

All of the answers are correct so far but I wanted to add my input.

For starters, we may be touching some unsupported customization here but that wasn't the question.

If you're running the script in context of a view, you cannot add jquery directly. The only way of accomplishing this is to load the file as an object from the resource url via your original script and append it to the head of the page. Then have an on-load that waits to execute the remaining script. This obviously is beyond what it should be and I advise against it. You're better off dropping jquery altogether in this scenario.

Lastly, if you're running it in context of the form (on the form), you can add the jquery to the entity's form as a normal script. Depending upon which form rendering engine you're using, you may need to do what Alex suggested and set $ = $ || parent.$;. If using the new turbo forms introduced in the newer versions, you will have to do this. Aside from that, you shouldn't have any problems using it from your ribbon.

Adding JQuery to your solution will add it as a resource. That alone will not allow you to execute it wherever you like.

hack3rfx
  • 573
  • 3
  • 16
  • That's a great reminder about unsupported customizations. There's some guidance [here](https://learn.microsoft.com/en-us/dynamics365/customerengagement/on-premises/customize/customizations-supported) that further clarifies. – Ben Drake - MSFT Jan 31 '20 at 15:00
3

When you want to use jQuery in your ribbon command, to make 100% sure jQuery is available, you should add an additional Custom Action in your RibbonCommand which takes place before your actual Custom Action in which your javascript is defined.

In this Custom Action you call the function isNaN on the jQuery webresource (which you also need to include).

Assuming you're using the Ribbon Workbench (which you probably should if you don't :-)), it would look like this:

enter image description here

Using this method you are sure that jQuery will be available, no matter what context you use (Forms, Grids, Subgrids).

If you do not actually add it like this, you need to rely on $ || parent.$, which works in 99% of the cases, but is not guaranteed to work. The solution mentioned by @Domenico will work, but only when the button is shown on Forms and the jQuery library is loaded before your custom code.

Nathan
  • 1,865
  • 4
  • 19
  • 25
2

Add the JQuery Library to CRM

  1. You can download the latest verison of JQuery from http://jquery.com/.

    1. After you have downloaded the JQuery you will want to add it to CRM as a JScript Web resource.
    2. Open your Solution or open the Default Solution by going to Settings > Custmoizations > Customize the System.
    3. Enter a name and Select Type: Script (JScript). I recomend also entering the JQuery file name in the descript so you know what version is installed.

    4. Click Browse and Select the JQuery file you just downloaded.

    5. Save and Publish the new Web Resource.

    6. You can now add the JQuery Library to your Forms so you can use it from your other Web Resources.
Domenico Zinzi
  • 954
  • 10
  • 18