Creating a Custom Action and Calling it from Web API in Dynamics 365 Power Apps

3 Comments

In Dynamics 365 and Power Apps, we can create Custom Actions and use these with plugins to invoke business-specific code through the Power Apps / Dynamics 365 Web API. These actions don’t even need any parameters to run and can contain no steps. They will simply execute our custom code, which can do whatever we want and we can invoke when we want.

In this example, let’s create a plugin that will create a contact called “Bob Smith” (simple example). Unlike other plugins, we won’t register this to run when a typical message such as a create/retrieve/update/delete runs on an entity. Instead, we will create a new custom Action in the process designer and will call it from JavaScript code through the Web API, showing we can have this new code run how we want it to (without binding it to an event).

First, let’s create a new plugin. Open Visual Studio and create a new Class Library.

Install the Microsoft.CrmSdk.CoreAssemblies using NuGet:

Rename the file to CustomAction (or whatever you want) and add the code below, where we are using IPlugin and code in our Execute method to create a new contact:

using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Carl.CustomAction
{
    public class CustomAction : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);

            // Create a new record
            Entity contact = new Entity("contact");
            contact["firstname"] = "Bob";
            contact["lastname"] = "Smith";
            Guid contactId = service.Create(contact);    
        }
    }
}

Sign the assembly and build the project. We now have plugin code ready to run.

Now, let’s create an action in Dynamics 365. Go to Processes->New:

We will call the action My Custom Action and the entity as None (global):

We don’t need to add any arguments or steps. Save it and Activate it like below:

Now, open the Plugin Registration Tool and register the assembly

Select the plugin code:

And a New Step:

Here we use the action we created as the message, i.e. new_MyCustomAction:

We will call it like below, on no entity:

If we were to look at the metadata in our org by browsing out to the Web API, we see our new action:

Now let’s call our action using JavaScript. We will use a simple piece of JS code to invoke our custom action:

    var req = {};
    req.getMetadata = function () {
        return {
            boundParameter: null,
            operationType: 0,
            operationName: "new_MyCustomAction"
        };
    };
 
    Xrm.WebApi.online.execute(req).then( 
        function (data) { 
            var e = data; 
            debugger; 
        }, 
        function (error) { 
            debugger; 
            var errMsg = error.message; 
        }
    );

We can run this code from the browser debugger:

On running this, we see our custom action has run successfully and has created a contact called Bob Smith:

We now have a service that we can call any time we want to perform the custom action.

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

3 Responses to Creating a Custom Action and Calling it from Web API in Dynamics 365 Power Apps

  1. Hi Carl de Souza,

    Your blog content in depth and awesome.

    I have a scenario where I wanted to provide a functionality using model driven app to the end user to select multiple select look up. I have gone through below link and it’s not working.

    Filter N:N Add Existing Lookup Dynamics 365 V9 Supported Code.

    https://blog.magnetismsolutions.com/blog/paulnieuwelaar/2018/05/17/filter-n-n-add-existing-lookup-dynamics-365-v9-supported-code

    Please could suggest or guide me how we can achieve this for model driven app in Power apps.

    Much appreciated.

    Regards
    Prasad Vasadi

Leave a Reply

Your email address will not be published. Required fields are marked *