This Information is copied from Chandan's CRM Blog, so for more information please visit Chandan Blog
===================================================================================================
-- Enable Associate and Disassociate Plug-in Events
-- execute the following query to CRM database, it will add two entries in SdkMessageFilterBase
-- 'DisassociateEntities' and 'AssociateEntities' from SdkMessageBase which is not listed
-- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
USE CrmDev_MSCRM
GO
-- Find the deployments SDK Filter ID for the
-- Associate and Disassociate Entity SDK Messages
DECLARE @DisassociateEntitiesFilterId uniqueidentifier
DECLARE @AssociateEntitiesFilterId uniqueidentifier
SET @DisassociateEntitiesFilterId = (SELECT SdkMessageId FROM SdkMessageBase WHERE [Name] = 'DisassociateEntities')
SET @AssociateEntitiesFilterId = (SELECT SdkMessageId FROM SdkMessageBase WHERE [Name] = 'AssociateEntities')
-- Enable the Associate and Disassociate Filters to be valid for custom processing
-- Custom Processing means "you register plug-ins against it"
-- Note: We only do this for the "generic" (OTC == 0) case, just to be safer
UPDATE SdkMessageFilterBase SET IsCustomProcessingStepAllowed = 1 WHERE SdkMessageId = @DisassociateEntitiesFilterId AND PrimaryObjectTypeCode = 0
UPDATE SdkMessageFilterBase SET IsCustomProcessingStepAllowed = 1 WHERE SdkMessageId = @AssociateEntitiesFilterId AND PrimaryObjectTypeCode = 0
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The above query will enable the 'DisassociateEntities' and 'AssociateEntities' message in the plugin registration tool. While registering the plugin select the entity name as ‘none’
While executing the plugin you will be getting four parameter as input parameters from the plugin context along with the other properties :
1. Related Entity1 guid and entity name
2. Related Entity2 guid and entity name
3. Relation hsip name (nothing but intersect hidden N:N entity name)
4. Optional parameter.
Using these value you can implement you logic.
Sample Code for N:N relationship with a custom entity and Sytem User:
public void Execute(IPluginExecutionContext context)
{
try
{
crmservice = context.CreateCrmService(true);
if (context.MessageName == "AssociateEntities")
{
if (context.InputParameters.Properties["RelationshipName"].ToString() == "new_new_customentity_systemuser")
{
string oId = string.Empty;
string SharedUserId = string.Empty;
oId = (context.InputParameters.Properties["Moniker1"] as Moniker).Id.ToString();
SharedUserId = (context.InputParameters.Properties["Moniker2"] as Moniker).Id.ToString();
// write you logic here
}
}
}
catch (System.Web.Services.Protocols.SoapException ex)
{
LogWriter.LogInfo(ex.ToString());
}
catch (Exception ex)
{
LogWriter.LogInfo(ex.ToString());
}
}
Note : As we have defined 'none' as the entity name while registering the plugin, so this plugin will get triggered when ever user will do add existing or romove existing, for any entity. So while writing the logic relationship name is required to identify from where plugin has been triggered.
===================================================================================================
-- Enable Associate and Disassociate Plug-in Events
-- execute the following query to CRM database, it will add two entries in SdkMessageFilterBase
-- 'DisassociateEntities' and 'AssociateEntities' from SdkMessageBase which is not listed
-- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
USE CrmDev_MSCRM
GO
-- Find the deployments SDK Filter ID for the
-- Associate and Disassociate Entity SDK Messages
DECLARE @DisassociateEntitiesFilterId uniqueidentifier
DECLARE @AssociateEntitiesFilterId uniqueidentifier
SET @DisassociateEntitiesFilterId = (SELECT SdkMessageId FROM SdkMessageBase WHERE [Name] = 'DisassociateEntities')
SET @AssociateEntitiesFilterId = (SELECT SdkMessageId FROM SdkMessageBase WHERE [Name] = 'AssociateEntities')
-- Enable the Associate and Disassociate Filters to be valid for custom processing
-- Custom Processing means "you register plug-ins against it"
-- Note: We only do this for the "generic" (OTC == 0) case, just to be safer
UPDATE SdkMessageFilterBase SET IsCustomProcessingStepAllowed = 1 WHERE SdkMessageId = @DisassociateEntitiesFilterId AND PrimaryObjectTypeCode = 0
UPDATE SdkMessageFilterBase SET IsCustomProcessingStepAllowed = 1 WHERE SdkMessageId = @AssociateEntitiesFilterId AND PrimaryObjectTypeCode = 0
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The above query will enable the 'DisassociateEntities' and 'AssociateEntities' message in the plugin registration tool. While registering the plugin select the entity name as ‘none’
While executing the plugin you will be getting four parameter as input parameters from the plugin context along with the other properties :
1. Related Entity1 guid and entity name
2. Related Entity2 guid and entity name
3. Relation hsip name (nothing but intersect hidden N:N entity name)
4. Optional parameter.
Using these value you can implement you logic.
Sample Code for N:N relationship with a custom entity and Sytem User:
public void Execute(IPluginExecutionContext context)
{
try
{
crmservice = context.CreateCrmService(true);
if (context.MessageName == "AssociateEntities")
{
if (context.InputParameters.Properties["RelationshipName"].ToString() == "new_new_customentity_systemuser")
{
string oId = string.Empty;
string SharedUserId = string.Empty;
oId = (context.InputParameters.Properties["Moniker1"] as Moniker).Id.ToString();
SharedUserId = (context.InputParameters.Properties["Moniker2"] as Moniker).Id.ToString();
// write you logic here
}
}
}
catch (System.Web.Services.Protocols.SoapException ex)
{
LogWriter.LogInfo(ex.ToString());
}
catch (Exception ex)
{
LogWriter.LogInfo(ex.ToString());
}
}
Note : As we have defined 'none' as the entity name while registering the plugin, so this plugin will get triggered when ever user will do add existing or romove existing, for any entity. So while writing the logic relationship name is required to identify from where plugin has been triggered.