locked
CRM 4.0 plugins, dynamicentity vs. moniker etc. RRS feed

  • Question

  • Hi everyone.

    I have developed plugins earlier and was able to do what I needed then. Now I need to work with plugins again and in regard to that I have some questions that I haven't been able to find a clear answer to.

    I'm interested in having my plugin detect: EntityName, MessageName, EntityId and to get the EntityImage (pre or post, which ever is available) preferable as DynamicEntity.

    I need to make the code short and universal so that it will detect any entity type and any message type.

    But I'm having problem determining when to look for DynamicEntity and when to look for Moniker (if at all).
    Which context collection to search in (InputParameters, OutputParameters, PostEntityImages, PreEntityImages etc.) and what to search for (Target, Id, Alias, PrimaryEntity etc.)

    If anyone can give a clear description of when to use what or even better, an example of something that will always work regardless of the entity and message, it would be much appreciated - thanks :)

    /Aidal

    Tuesday, October 19, 2010 9:32 AM

Answers

  • You can test whether or not the InputParameters collection "contains" something, and you can also test an object's type by using an "if" statement that uses the "is" keyword.  A la:

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is DynamicEntity)
    {
     // Work with the DynamicEntity
    }
    
    if (context.InputParameters.Contains("EntityMoniker") && context.InputParameters["EntityMoniker"] is Moniker)
    {
     // Work with the Moniker
    }
    

    You may be able to spool through the InputParameters, PreEntityImages, and PostEntityImages collections using this code (but I haven't tested it, so your mileage may vary):

    foreach (PropertyBagEntry property in context.InputParameters) {
     if (property.Value is DynamicEntity) {
      // The property is a DynamicEntity
      // property.Name can be tested to evaluate as "Target"
     }
     else if (property.Value is Moniker) {
      // The property is a Moniker
      // property.Name can be tested to evaluate as "EntityMoniker"
     }
    }
    


    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com
    Tuesday, October 19, 2010 10:59 PM
    Moderator
  • Just want to add few more things. With Moniker, we can only get the "Id" and "Name" of instance of the "Target" entity. For few of the Messages(assign, SetStateDynamicEntity, Retrieve, GrantAccess...)we have "Moniker" as the only "Target" in the "InputParameters". So for those messages we need to use "Moniker" instead of "DynamicEntity". With "context.MessageName" we will get the "Message" that triggered the plugin. Using that you can decide whether to use "DynamicEntity" or "Moniker". Below is one of the useful post which talks about Plugin Parameters which will be passed to the plugins

    http://mscrmuk.blogspot.com/2008/06/plugin-parameters.html 

    For the coding stuff, you need to consider the above approach given by David. If I am not wrong, I believe you are looking to implement the stuff which is similiar to the "Audit Plugin". You can get that from here: http://crmaudit.codeplex.com . Even if you it doesn't suits to your requirement, you can have a look at the source code so that it will be more useful for your requirement.


    Vicky http://howto-mscrm.blogspot.com "Please Mark it as answer if it helps in resolving your query"
    Wednesday, October 20, 2010 6:28 AM

All replies

  • You can test whether or not the InputParameters collection "contains" something, and you can also test an object's type by using an "if" statement that uses the "is" keyword.  A la:

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is DynamicEntity)
    {
     // Work with the DynamicEntity
    }
    
    if (context.InputParameters.Contains("EntityMoniker") && context.InputParameters["EntityMoniker"] is Moniker)
    {
     // Work with the Moniker
    }
    

    You may be able to spool through the InputParameters, PreEntityImages, and PostEntityImages collections using this code (but I haven't tested it, so your mileage may vary):

    foreach (PropertyBagEntry property in context.InputParameters) {
     if (property.Value is DynamicEntity) {
      // The property is a DynamicEntity
      // property.Name can be tested to evaluate as "Target"
     }
     else if (property.Value is Moniker) {
      // The property is a Moniker
      // property.Name can be tested to evaluate as "EntityMoniker"
     }
    }
    


    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com
    Tuesday, October 19, 2010 10:59 PM
    Moderator
  • Just want to add few more things. With Moniker, we can only get the "Id" and "Name" of instance of the "Target" entity. For few of the Messages(assign, SetStateDynamicEntity, Retrieve, GrantAccess...)we have "Moniker" as the only "Target" in the "InputParameters". So for those messages we need to use "Moniker" instead of "DynamicEntity". With "context.MessageName" we will get the "Message" that triggered the plugin. Using that you can decide whether to use "DynamicEntity" or "Moniker". Below is one of the useful post which talks about Plugin Parameters which will be passed to the plugins

    http://mscrmuk.blogspot.com/2008/06/plugin-parameters.html 

    For the coding stuff, you need to consider the above approach given by David. If I am not wrong, I believe you are looking to implement the stuff which is similiar to the "Audit Plugin". You can get that from here: http://crmaudit.codeplex.com . Even if you it doesn't suits to your requirement, you can have a look at the source code so that it will be more useful for your requirement.


    Vicky http://howto-mscrm.blogspot.com "Please Mark it as answer if it helps in resolving your query"
    Wednesday, October 20, 2010 6:28 AM