Call custom action using WebApi in PowerApps portals/ dynamics 365 portals.

So here I am in the last blog of this series in which I covered the various web api operations we can perform from PowerApps portals. If you are new to the concept of WebAPI in PowerApps portals, I strongly suggest you read this series from the beginning. However if you are looking for specifically on how to call an action using WebApi from portals, continue reading.

For this example, I am using a custom action bound to contact entity. Below is the snapshot of the action.

Bound Action

As seen from the above screenshot, we have two input parameters and one output parameters. The action is bound to contact entity. To call the action, we actually need to perform the same operation we performed in our previous blog. To call a bound action, you should configure the same site settings you configure while calling a WebApi for create/ update/ delete operation.

Setting 1

The first setting is to enable webapi support for an entity in the portal. To do that, create the below site setting. If the site setting is already present, we don’t need to create it.

Setting 2

This setting is to enable fields which will be used by WebApi. This is probably the most important step and missed by developers while configuring a custom action from WebApi.

Observe the highlighted field – “StringParam”. This is the same parameter which is used in the action as input parameter. Although it is not a field of contact entity, but any action called on the contact entity should register the input parameter in the field list.

If you are not including the parameter in the request, the parameter can be skipped in the list of fields.

All set and done, it’s now time to clear the portal cache and browse the portal. Below is the sample code to invoke the action.

shell.getTokenDeferred().done(function (s) {
  
   var inputJson = {};
   inputJson.StringParam = "Invoking custom action from Portal";

   var inputstring = JSON.stringify(inputJson);

   var url = "<your_portal_url>/_api/contacts(1715cd14-24de-ea11-a814-000d3a0a7552)/Microsoft.Dynamics.CRM.new_WebApiAction";
   var xmlHttp = new XMLHttpRequest();
   xmlHttp.open("POST", url
      , true);
   xmlHttp.setRequestHeader('Content-Type', 'application/json');
   xmlHttp.setRequestHeader('__RequestVerificationToken', s);

   xmlHttp.onreadystatechange = function () {
      if (this.readyState === 4) {
         if (this.status === 200) {
            // your custom code goes here;
         }
         else {
            //you custom code goes here;
         }
      }
   };

   xmlHttp.send(inputstring);

});

Observe that I didn’t include the Entity reference input parameter in the action. If I include that, I need to include that in my field list as well.

And before I end, don’t forget the entity permission on the contact entity. Even though the action is not retrieving the contact entity information through the action, to call an action bound to contact entity, you must have entity permissions enabled for the entity. The same applies to any other entity as well.

Hope that helps!
Debajit Dutta
(Business Solutions MVP)

3 thoughts on “Call custom action using WebApi in PowerApps portals/ dynamics 365 portals.”

  1. I have used same code , but getting 404 not found error. Can you please suggest me what can be the issue.

    $(‘#confirmAndPay’).click(function(s) {
    shell.getTokenDeferred().done(function (s) {

    var inputJson = {};
    inputJson.StringParam = “Invoking custom action from Portal”;

    var inputstring = JSON.stringify(inputJson);

    var url = “https://ihttc.powerappsportals.com/_api/contacts(b5f785ea-62ee-ea11-a815-000d3a86a3ce/Microsoft.Dynamics.CRM.ftts_ActiontocallinPAPPOC”;
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open(“POST”, url
    , true);
    xmlHttp.setRequestHeader(‘Content-Type’, ‘application/json’);
    xmlHttp.setRequestHeader(‘__RequestVerificationToken’, s);

    xmlHttp.onreadystatechange = function () {
    if (this.readyState === 4) {
    if (this.status === 200) {
    var results = JSON.parse(this.response);
    alert(“Successful action call”);
    }
    else {
    alert(“Unsuccessful action call”);
    }
    }
    };

    xmlHttp.send(inputstring);

    });
    });

    1. Hi,
      Thanks for reaching out. My first hunch, you are missing bracket at the end of the guid
      var url = “https://ihttc.powerappsportals.com/_api/contacts(b5f785ea-62ee-ea11-a815-000d3a86a3ce/Microsoft.Dynamics.CRM.ftts_ActiontocallinPAPPOC”;

      -Debajit

      1. Thank you for the response, i had fixed that. There was some entity permission issue.
        But now we are nor getting the response .If we are using JSON.Parse(this.response) or JSON.Parse(xmlHttp.response) getting null response.
        yeah if we are trying to do CRUD operation in action that is happening.
        Can you please suggest something why not getting any response.

        Thanks

Comments are closed.