简体   繁体   中英

Programmatically register an OnLoad handler (for a form) in Dynamics CRM 2011

Is there a way to register an OnLoad() handler in code?

Ex:

A script is added as a Form Library (Customize Form etc.). It contains code that wants to be executed only after the form is loaded. I suppose it can be done by using setTimeout() / setInterval() - and check Xrm.Page.data object or so, but is there another way?

First, why do you want to do it programmatically ?

  • Form properties -> Event Handlers -> OnLoad -> Add -> put the function you want to execute after the form is loaded.

Otherwise, you can do it programmatically by using jQuery. Like this :

$(document).ready(function() {
     // your code
});

Of course, you will have to add jQuery in your form's libraries.

Be careful using jQuery ready event. The data is retrieve asynchronously. The jQuery ready event may fire before the CRM is finished loading.

A very old question and answer, but so very wrong. Please don't do this using jQuery. Form events are registered in the formxml. If you want your code to work as part of a solution, you need to update the formxml in the same way that the UI would.

I know this question is very old, and we have CRM 2016 now. Recently while trying to fix a bug in CRM 2016. https://community.dynamics.com/crm/f/117/t/186877

I had the requirement to call/add a function eg function abc() OnLoad of All Forms of All Entities .

I have done it in below way.Hope this helps someone.

  1. Download XrmToolBox from https://www.xrmtoolbox.com/
  2. Using the Form Libraries Manager plugin in XrmToolBox add the JS files where abc() function is to all forms in CRM or Selected if you want as below.
  3. In your JS file which you added to all Forms, add the following at first line.

     setTimeout(function () { abc(); }, 1500); 

在此输入图像描述

Using the above procedure will CALL the abc() on Load of everyform without manually adding handlers on each and every form.

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