CRM Blog

Valcore ApS Logo (c) Valcore ApS 2012

Dynamics Dev Tweaks

To share my solutions/tweaks while developing solutions for dynamics 365 problems that i thing might be helpful / interesting for the audience

Button(Ribbon) to Call plugin (pass parameters and receive results)

At some point on the roadmap of every Dynamics 365 (CRM) developer's life, one must have wished to press a button to call a Plugin. 
A combination of Custom Actions introduced in CRM 2013 and webapi introduced in CRM 2016 made this wish very easy. Developers have used different techniques to achieve the same goal e.g. creating records of dummy enttities or updating dummy fields of an entity to call plugins.
There is a smarter way to do so using Custom Actions and here is how we can do it:
Our solution consists of 4 main parts:

1. A CRM custom action with input/output parameters (it can be bound(on an entity) or unbound(global) depending on your requirements)
2. A plugin registered on the custom action (this will perform the task and will set the outpur parameter with results)
3. Script under the button: this will use webapi to call custom action sending input parameters if necessary and will receive the results as output paramters
4. Ribbon button to call the javascript function

User presses a button on a form which fires a javascript function. JS function uses webapi to call the Custom Action and passes input parameters e.g. record's guid. Plugin registered on this custom action gets fired as soon the custom action runs. Plugin gets input parameters and perform actions and finally set the output parameters to be returned. JS function gets the results from the output parameters set by plugin after the plugin finishes processing.

Here is a Sample Javascript function which calls custom action "val_CASCA" passing input parameter "InParameter1" and gets results in output parameter "OutParameter1" set by plugin after processing.

function CallCASCE() {
   var Id ='{', '').replace('}', '');
   var serverURL = Xrm.Page.context.getClientUrl();    
   var data = {
       "InParameter1": Id
   var req = new XMLHttpRequest();
   // specify name of the entity, record id and name of the action in the Wen API Url"POST", serverURL + "/api/data/v8.2/val_scas(" + Id + ")/Microsoft.Dynamics.CRM.val_CASCA", false);
   req.setRequestHeader("Accept", "application/json");
   req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
   req.setRequestHeader("OData-MaxVersion", "4.0");
   req.setRequestHeader("OData-Version", "4.0");
   req.onreadystatechange = function () {
       if (this.readyState == 4 /* complete */) {
           req.onreadystatechange = null;
           if (this.status == 200) {
               var result = JSON.parse(this.response);
           } else {
               var error = JSON.parse(this.response).error;
   // send the request with the data for the input parameter

Here is the sample Custom Action "val_CASCA" which is bound to custom entity "SCA" have one imput parameter and one output parameter of types string.

Finally the plugin registerd on this custom action.

public void Execute(IServiceProvider serviceProvider)


string results = string.Empty;
if ((context.InputParameters["Target"] is EntityReference)) { results += "InputParamters[Target] is entityreference"; }
try {

var inParam1 = context.InputParameters["InParameter1"];
if (inParam1.GetType() == typeof(string))
results +=" Input parameter sent from script is of type string:"+ inParam1.ToString();
context.OutputParameters["OutParameter1"] = results;

catch (Exception ex)

throw new InvalidPluginExecutionException("Error occured in [CustomActionPlugin]:" + ex.Message, ex);



 Note: Plugin must be registered on Post operation, otherwise we will not get the parameters passed. Both webapi call and plugin are synchronous to acheive realtime results. We can show a modal popup (processing...) mens webapi requies is processing i.e. plugin is running.

As we can see that in case of bound custom actions we even get the entityreference as target input parameter. we get the InParameter1 set in javascript while calling customAction, perform operations and return the results in output parameter.

Here is a screenshot of the button(CallActionPlugin) and its results in alertbox sent from plugin.


Hope it will help specially new developers :)


Using CRM Actions to overcome workflow limitation ...
Hvorfor din CRM-visning ikke altid virker.....


No comments made yet. Be the first to submit a comment
Vores adresse:
Industrivej 48
4000 Roskilde, Danmark
Telefon: +45 70 22 14 64
E-mail: Denne e-mail adresse bliver beskyttet mod spambots. Du skal have JavaScript aktiveret for at vise den.
CVR nr.: 34472726
Mandag til Torsdag kl. 09.00 - 16.00
Fredag kl. 09.00 - 15.30
Weekend lukket

Kig forbi!

Hos Valcore er du altid velkommen til at kigge forbi til en kop kaffe og en gennemgang af Microsoft Dynamics CRM. Vi brænder for CRM og vi er gode til at skabe en god forståelse for hvordan CRM kan bruges i en virksomhed, hvad enten der er fokus på salg, marketing, service eller noget helt andet!