Wednesday, November 9, 2011

How To Trigger a Plug-in From JScript in Microsoft Dynamics CRM

From my time in the Microsoft Dynamics CRM forums, I can tell you that the question of how to trigger a plug-in from jscript comes up pretty frequently.  Usually someone wants to just trigger the plug-in from a form event, but also sometimes people want to trigger the plug-in from a ribbon button.  Unfortunately, a lot of the replies usually say that it's not supported or not possible. I am happy to say that it is possible and that I have regularly used this workaround to achieve this and it even uses supported methodology.

The code examples I link to today will be for CRM 2011 but the basic methodology is the same for 4.0 also.

Here is how this works:

1. First you need to create a new custom entity to act as a trigger entity, I'll often times even name this new custom entity "new_trigger{plugin name}"

2. Now you must register your plug-in (or several plug-ins) on the create or update messages for this new entity. (I usually prefer the create message for my registrations because a new record will be created which keeps a record or audit-trail of who manually executed your plug-in and when in the created by and created time-stamp attributes on the entity)

3.  You must create a jscript web resource that contains a function that performs a soap update or create call on the entity depending on which message you registered your plug-in on.

4.  Lastly you must add your web resource to your form and call your update or create jscript function on one of the exposed form events (OnLoad, OnSave, etc...).  You can also call your jscript function from ribbon button if you want.

I hope this helps!



  1. We also used a different approach but somewhat similar.

    we had a flag on the entity and from the Jscript we update the flag and also register the plugin on the update of this flag.

  2. That would be another way to approach the problem. I just don't like the answer that I see in the forums all too often that it cannot be done. :)

  3. I have implemented this solution, the call is working, but I can't get any data back out to javascript.
    If anyone can help, I have posted this on stackoverflow