简体   繁体   中英

Dynamics CRM - How to use JQuery in a Javascript Web ressource

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 ?

您应该能够像这样访问 jQuery:

$ = ($ || parent.$);

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.$; $ = $ || 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.

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:

在此处输入图片说明

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM