Skip to main content

When calling the Dynamics 365 Power Apps Web Api, you may have a need to pass a custom value along with the request. For example, let’s say you’re retrieving a list of contacts through a Web Api call, and you want to pass along a custom value CallerId so you know who is making the Web Api call. In this post, we will look at how to the tag property in Web Api can be used to pass custom values to plugins. We will also take a look at Shared Variables and how they come into play.

First, let’s set up a Web Api call to retrieve all contacts in the system:

The URL: /api/data/v9.2/contacts

Now, to pass a tag, we can simply add tag to the web api request:

/api/data/v9.2/contacts?tag=Hello World

The first thing to note is the tag will show up in the plugin’s Shared Variables under the execution context. Shared Variables are useful in that you can use the property to pass information between each stage of the execution pipeline, it’s not exclusive to tags coming from the web api. We will show an example below. When we’re in the plugin, we will be able to see the tag value, and we can then act accordingly, e.g. using the tag’s value to execute or change business logic.

Let’s see how this tag looks when we pass it into the plugin execution pipeline. We will look at the tag in each of the 3 plugins that run in order:

  1. Pre-validation
  2. Pre-operation
  3. Post-operation

We will add some pre-validation code to display all the shared variables. So in the pre-validation step, we would expect to see in SharedVariables the tag value we pass, in the above case “Hello World”. We will also add some code to create a NewSharedVariable key/value:

using Microsoft.Xrm.Sdk;
using System; namespace Carl.Contact
{ public class ContactPreValidation : IPlugin { public void Execute(IServiceProvider serviceProvider) { ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); IPluginExecutionContext executionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); tracingService.Trace("PRE-VALIDATION"); if (executionContext.SharedVariables != null) { tracingService.Trace($"tag: {executionContext.SharedVariables["tag"].ToString()}"); tracingService.Trace("Values: "); foreach (var i in executionContext.SharedVariables.Values) { tracingService.Trace(i.ToString()); } } // Add a new Shared Variable executionContext.SharedVariables.Add("NewSharedVariable", "test"); tracingService.Trace($"NewSharedVariable: {executionContext.SharedVariables["NewSharedVariable"].ToString()}"); } }
}

Now in the pre-operation plugin, let’s add the code below. We will again look at the SharedVariables that are available to us in the pre-operation, and let’s also change the NewSharedVariable to a different value, so we see its new value in the post operation. Note I did try to change the tag, but it seems this is not possible, which I don’t think should be a showstopper for any use case as you can utilize your own SharedVariables anyway:

using Microsoft.Xrm.Sdk;
using System; namespace Carl.Contact
{ public class ContactPreOperation : IPlugin { public void Execute(IServiceProvider serviceProvider) { ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); IPluginExecutionContext executionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); tracingService.Trace("PRE-OPERATION"); tracingService.Trace($"tag: {executionContext.SharedVariables["tag"].ToString()}"); tracingService.Trace("Values: "); foreach (var i in executionContext.SharedVariables.Values) { tracingService.Trace(i.ToString()); } // Change Shared Variable executionContext.SharedVariables["NewSharedVariable"] = "test2"; tracingService.Trace($"NewSharedVariable: {executionContext.SharedVariables["NewSharedVariable"].ToString()}"); } }
}

Finally, let’s look at the post-operation values:

using Microsoft.Xrm.Sdk;
using System; namespace Carl.Contact
{ public class ContactPostOperation : IPlugin { public void Execute(IServiceProvider serviceProvider) { ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); IPluginExecutionContext executionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); tracingService.Trace("POST-OPERATION"); tracingService.Trace($"tag: {executionContext.SharedVariables["tag"].ToString()}"); tracingService.Trace("Values: "); foreach (var i in executionContext.SharedVariables.Values) { tracingService.Trace(i.ToString()); } tracingService.Trace($"NewSharedVariable: {executionContext.SharedVariables["NewSharedVariable"].ToString()}"); } }
}

When we run the web api:

We see the following in the message block for pre-validation. We see the Hello World value coming in with the tag. Note there is another key in SharedVariables at this point with a value of False, which we are not passing ourselves. We can ignore that. We also see our NewSharedVariable that we created in the plugin is set to “test”:

Now the pre-operation. We see the tag “Hello World” is still available, and we are setting our NewSharedVariable to a new value of test2. Let’s see if that carries through to the post-operation:

The post-operation looks like this. We see the updated NewSharedVariable, and we also have the tag value available:

Now, what if you want to pass more than one value in the tag? You could utilize JSON like below:

Request: api/data/v9.2/contacts?tag={“name”:”John”,%20″age”:30,%20″car”:null}

Now that the JSON appears in the plugin, we can utilize it as we need to get the individual values passed:

Hopefully you find this useful when using the Dynamics 365 / Power Apps web api.

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

Source