dynamics-crmdynamics-crm-onlinedynamics-crm-2016

How to know what InputParameters values are possible in Dynamics CRM Plugin context?


I'm trying to understand the plug-in sample from here. There's this condition:

// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)

Speaking generally, not just with regard to this sample, on what prior knowledge should I base my decision to access a specific property? How could I have known to test whether the InputParameters contains a "Target" key (I assume I'm not supposed to guess it)?

And on what basis could I have known to ask whether the "Target" mapped value is of Entity type, and not some other type?

I found this post from 2 years ago, and I've found this webpage, saying (emphasis is mine):

Within a plugin, the values in context.InputParameters and context.OutputParameters depend on the message and the stage that you register the plugin on. For example, "Target" is present in InputParameters for the Create and Update messages, but not the SetState message. Also, OutputParameters only exist in a Post stage, and not in a Pre stage. There is no single source of documentation that provides the complete set of InputParameters and OutputParameters by message and stage.

From my searchings, a single source still doesn't exist, but maybe the possible values can be found using the Dynamics Online platform, somewhere deep down the Settings menu, maybe? Any source would be great.


Solution

  • The best practice for doing this is to use a strongly typed approach. If, for example, you want to know which propertes are available on a CreateRequest, you would do:

    var createReq = new CreateRequest() { Parameters = context.InputParameters };
    createReq.Target; // Has type Entity
    

    Take a look at the full blog post explaining this approach: Tip: Proper handling of Plugin InputParameters


    Original answer:

    It depends on which request we are talking about. See Understand the data context passed to a plug-in on MSDN.

    As an example, take a look at CreateRequest. One property of CreateRequest is named Target, which is of type Entity. This is the entity currently being operated upon by the platform. To access the data of the entity you would use the name “Target” as the key in the input parameter collection. You also need to cast the returned instance.

    Note that not all requests contain a Target property that is of type Entity, so you have to look at each request or response. For example, DeleteRequest has a Target property, but its type is EntityReference.

    In summary: Look at the actual request, e.g the CreateRequest.