I’ve Been Doing Plugin Parameters Wrong For 7 Years
I’ve been an active Stack Overflow (SO) user for about 7 years now. There are 3 ways to increase your knowledge from SO.
- Being able to ask a question and get an answer in hours. It’s not always the correct, or the answer you want, but more often than not, it is a great way to increase your knowledge. This though, is not the primary conduit to increasing your knowledge, that privilege belongs to way #2.
- Finding the answers to the questions that have already been asked, and instantaneously getting the knowledge you want (sometimes by the former, “smarter” you). This is by far the most common method of learning on SO. It also brings up a new not quite as beneficial coding paradigms (Stack-Overflow-Copy-and-Paste-Yourself-to-Victory). If this second option is the bread and butter of learning for developers today, then third option is the dessert, the unexpected delightfulness that makes the SO community a great place to contribute to, and learn from.
- Providing an answer to a question that you believe to be 100% correct, but then having someone else respond with an even better/more awesome/even more “correcter” answer, to which you tweet about, and then someone else responds with an even better/more awesome/even more “correcter”/more ludicrous answer. This third option is the subject of today’s post.
- I posted an answer to a SO question about plugin parameters almost 6 months ago (Feel free to refer to the SO question mentioned here: How to know what InputParameters values are possible in Dynamics CRM Plugin context?).
- Then Federico Jousset commented on my answer with a helper web page that he has created that is a better tool, IMHO, to answer the OP’s question.
- I then tweeted about it (since this knowledge should be shared among the global Dynamics 365 community).
- Martin Tange responded with a blog post (which I’m assuming he wrote in direct response to my tweet) that brought to light how I’ve been doing plugin parameters wrong since day one.
- And now you're here learning about it as well!
The MSDN documented approach to accessing parameters is to use “magic” strings and assumed casting
// The InputParameters collection contains all the data passed in the message request.Martin’s technique is so simple, I can't believe I haven't seen it used before:
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
{
// Obtain the target entity from the input parameters.
EntityReference entity = (EntityReference)context.InputParameters["Target"];
}
var createReq = new CreateRequest { Parameters = context.InputParameters };To which I’ve wrapped in an extension method to help remove some additional key strokes (which I’ve added to the DLaB.Xrm.Plugin namespace):
createReq.Target; // Is typed as Entity vs EntityReference (DeleteRequest)
/// <summary>So now, this is even shorter:
/// Populates a local version of the request using the parameters from the context. This exposes (most of) the parameters of that particular request
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="context">The context.</param>
/// <returns></returns>
public static T GetRequestParameters<T>(this IPluginExecutionContext context) where T: OrganizationRequest
{
var request = Activator.CreateInstance<T>();
request.Parameters = context.InputParameters;
return request;
}
/// <summary>
/// Populates a local version of the response using the parameters from the context. This exposes (most of) the parameters of that particular response
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="context">The context.</param>
/// <returns></returns>
public static T GetResponseParameters<T>(this IPluginExecutionContext context) where T : OrganizationResponse
{
var response = Activator.CreateInstance<T>();
response.Results = context.OutputParameters;
return response;
}
var parameters = context.GetRequestParameters<CreateRequest>();So my challenge to you dear reader, get involved in your community. You may never know when an attempt to share your knowledge with someone else, ends up unexpectedly resulting in others sharing their knowledge with you!
parameters.Target;
Published on:
Learn moreRelated posts
PowerFx Formatting If Statement Idiosyncrasies
Disclaimer: This entire blog is an opinion, and opinions are like butts, we all have one. This is not doctrinal truth that must be obser...
Separating Plugin Logic: A Guide to Testing Dataverse Plugins with IOC
I’m not a pure TDD developer. I frequently take my best guess at a Dataverse plugin, then apply TDD until everything works. This c...
How to Filter Dates in Canvas Apps Using Greater Than/Less Than Operators
Defining the ProblemRecently I was attempting to filter an on-premise SQL table by a DateTime field using a “greater than” operator, and displ...
Enabling or Disabling All Plugin Steps In Dataverse
If you are facing an issue with a recent bug in PowerPlatform.BuildTools version 0.0.81 that caused your plugin steps to become disabled, fret...
Using AutoFixture To Create Early Bound Entities
AutoFixture is an open source library that is used in testing to create objects without having to explicit set all the values. I recentl...
Long Functions Are Always A Code Smell
This article is in response to fellow MVP Alex Shelga’s recent article Long functions in dataverse plugins – is it still ” code smell”?. ...
Handle All Your Plugin Exceptions In One Place, And Then Hide It!
In coding, one of the recommended best practices is to avoid creating duplicate code as it leads to maintenance issues. Therefore, it is advis...