Associate/Disassociate plugin messages in CRM


Most of the times to fulfill our requirement, we will creating N:N relation ship between the entities. Sometimes, we might need to perform some business logic which relating two records. For implementing we will have to write plugins and register them on “Associate” message.


Here is something interesting, the Associate and Disassociate messages behave little different that other messages.

When we register a plugin on Associate or Disassociate message, we have to leave Primary and Secondary entities as ‘None’. As we haven’t specified any entities, the plugin triggers on all Associate/Disassociate operations. We need to check few conditions to let the Association happen only between required entities.

Here is the sample which is like template for Associate/Disassociate message.

EntityReference targetEntity = null;
string strRelationshipName = string.Empty;
EntityReferenceCollection relatedEntities = null;
EntityReference relatedEntity = null;
if (pluginContext.MessageName == “Associate”)
    // Get the “Relationship” Key from context
        strRelationshipName = pluginContext.InputParameters[“Relationship”].ToString();
    // Check the “Relationship Name” with your intended one
    if (strRelationshipName != “”)
    // Get Entity 1 reference from “Target” Key from context
    if(pluginContext.InputParameters.Contains(“Target”) && pluginContext.InputParameters[“Target”] is EntityReference)
        targetEntity = (EntityReference)pluginContext.InputParameters[“Target”];
    // Get Entity 2 reference from “RelatedEntities” Key from context
    if(pluginContext.InputParameters.Contains(“RelatedEntities”) && pluginContext.InputParameters[“RelatedEntities”] isEntityReferenceCollection)
        relatedEntities = pluginContext.InputParameters[“RelatedEntities”] asEntityReferenceCollection;
        relatedEntity = relatedEntities[0];

Hope this helps.

Happy CRM’ing


2 thoughts on “Associate/Disassociate plugin messages in CRM

  1. Larry McCoy January 4, 2018 / 11:39 pm

    The above code will likely have problems in later versions of CRM. The value returned from the call to 'InputParameters[\”Relationship\”]' returns an object of type Microsoft.Xrm.Sdk.Relationship. I have found that calling \”ToString()\” on this object gives unpredictable results. Often the string returned has an additional \”.\” (period) character at the end (when being called on Associate). On teh Dissassociate message, the ToString() value includes some additional odd characters. For example, my relationship name is \”new_office_new_address\”. In the associate call the value returned by ToString() was:\”new_office_new_address.\”In the disassociate call the value returned by ToString() was:\”new_office_new_address.Referenced\”I am testing with Dynamics 365 (8.2) on premise, on a system that was originally for Dynamics CRM 2011, so code that worked before is now broken due to this odd behavior.Just a friendly note to watch out for the change in behavior…


  2. Gopinath January 8, 2018 / 2:59 pm

    Thanks Larry, will check and update the post accordingly.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s