When I scanned Microsoft.Xrm.SDK namespace from Visual Studio, I found an exciting extension in the ContextExtensions class. In there, we can discover InputParameterOrDefault<T> that accepts a string parameter. Without further a do, let’s go to my testing about it!
Demo 😁
I created a WebApi (testing) with lots of parameters like the below (I created the Custom API using Custom API Manager by David Rivard – XrmToolBox):

Then for the code itself:
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Extensions;
namespace DemoPlugin
{
public class TestCustomApi : PluginBase, IPlugin
{
public TestCustomApi() : base(typeof(TestCustomApi))
{
}
public void Execute(IServiceProvider serviceProvider)
{
var pluginExecutionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var random = pluginExecutionContext.InputParameterOrDefault<string>("check-null");
var request = pluginExecutionContext.InputParameterOrDefault<string>("Request");
var requestDateTime = pluginExecutionContext.InputParameterOrDefault<DateTime>("RequestDateTime");
var requestEntity = pluginExecutionContext.InputParameterOrDefault<Entity>("RequestEntity");
var requestMoney = pluginExecutionContext.InputParameterOrDefault<Money>("RequestMoney");
var requestBool = pluginExecutionContext.InputParameterOrDefault<bool>("RequestBool");
var requestFloat = pluginExecutionContext.InputParameterOrDefault<decimal>("RequestFloat");
var requestInt = pluginExecutionContext.InputParameterOrDefault<int>("RequestInt");
var requestEntityCollection = pluginExecutionContext.InputParameterOrDefault<EntityCollection>("RequestEntityCollection");
var requestEntityReference =
pluginExecutionContext.InputParameterOrDefault<EntityReference>("RequestEntityReference");
pluginExecutionContext.OutputParameters["Response"] = string.Join(Environment.NewLine,
new[]
{
$"String: {request}",
$"DateTime: {requestDateTime}",
$"Entity: Id-{requestEntity.Id}:LogicalName-{requestEntity.LogicalName}",
$"EntityReference: Id-{requestEntityReference.Id}:Name-{requestEntityReference.LogicalName}-{requestEntityReference.Name}",
$"EntityCollection: {requestEntityCollection?.Entities?.Count}",
$"Money: {requestMoney.Value}",
$"Bool: {requestBool}",
$"Float: {requestFloat}",
$"Integer: {requestInt}",
$"Test-Null: {random ?? "Empty"}"
});
}
}
}
The API will accept all the parameters and construct the predefined string as the return. And for the result, you can see the below image (I testing using Custom API Tester by Jonas Rapp – XrmToolBox):

On line 17 (from the code), I test with a random parameter (that will not exist when we call it). The code will not invoke NullReferenceException which is totally a game-changer for you to always use in the next project. 😎
Summary
Today’s blog post is quite simple and just shows how we can use IPluginExecutionContext‘s extension method (InputParameterOrDefault). Normally, we can see the below sample of code in the plugin:
var pluginExecutionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var target = pluginExecutionContext.InputParameters.Contains("Target") ?
(Entity)pluginExecutionContext.InputParameters["Target"] : null;
We can change to:
var pluginExecutionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var target = pluginExecutionContext.InputParameterOrDefault<Entity>("Target");
Instead of checking if the InputParameter exists and casting the object to Entity (else set to null), we can directly use InputParameterOrDefault and set the object to Entity.
Happy CRM-ing!