I find your lack of events disturbing. 

It has been a long time since I have posted a nice juicy hack to CRM, but I have a good one for today.  For developers out there that have been happily coding plug-ins for Microsoft Dynamics CRM 4.0 and have had the desire to also write plug-ins that fire when you Associate or Disassociate entities via a many-to-many (N:N) relationship; this hack is for you.  First let’s talk about the feature.  Microsoft Dynamics CRM 4.0 added a new “Many to Many” relationship type that allows you to associate one entity to another multiple times and vice versa.  This relationship type also existed in CRM 3.0, via a bridge entity and 1:N <– Linker -> N:1 relationship (which is still available if you need to store additional details on the link).  The new feature in CRM 4.0 still uses a bridge or “linker” entity to store the relationship, but this special entity is hidden from the user and has no form or additional attributes; users instead use a handy associate dialog to do the work.

Here is a many-to-many grid in CRM 4.0:

Screen01  

Let say I want to fire a plug-in when users removes this association:

Screen02

After applying my hack you will now see this in Plug-in Registration Tool

Screen04

And now you can write a plug-in and get all the business you would ever want:

Screen03

Here is the code for the hack.  Use this at your own risk!!!

-- ============================================================================
-- Enable Associate and Disassociate Plug-in Events Script v1.0
-- ----------------------------------------------------------------------------
-- (c) 2009 Aaron Elder
-- ============================================================================
-- DISCLAIMER:
-- This script is provided "AS IS" with no warranties, and confers no rights.
-- ============================================================================
-- While this is obviously "unsupported", I think the fact that these events
-- are not available is a bug and hopefully it will be fixed in a rollup.
-- ============================================================================

USE AscentiumCrmDev_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

Fixed column names – Thanks Dane

After applying the hack, simply do an IISRESET to flush the metadata cache and if you have Plug-in Registration running, tell it to refresh your Organization as well.

Is this hack safe to use?  The answer is maybe.  Internally, the event model that fires this plug-in is the exact same as any other plug-in.  My understanding is that due to a lack of time, this additional event simply did not get fully tested and as such Microsoft wanted to be more safe than sorry.  Cheers,

 

This posting is provided "AS IS" with no warranties, and confers no rights.

Comments
Hi Aaron,

Nice stuff. I have been going through same issue last year and at that time it was a lacking by design. Will try your new fix.

Thanks for sharing.

Ayaz
brilliant as usual - just came in at the right time. Thanks Aaron.
Hi Aaron,

Really Nice Hack. Tried implementing it but got errors. Made some changes and seems to work. Is this correct on your side as well?

-- ============================================================================
-- Enable Associate and Disassociate Plug-in Events Script v1.0
-- ----------------------------------------------------------------------------
-- (c) 2009 Aaron Elder
-- ============================================================================
-- DISCLAIMER:
-- This script is provided "AS IS" with no warranties, and confers no rights.
-- ============================================================================
-- While this is obviously "unsupported", I think the fact that these events
-- are not available is a bug and hopefully it will be fixed in a rollup.
-- ============================================================================

USE migratetest3_MSCRM
GO

-- Find the deployments SDK Filter ID for the
-- Associate and Disassociate Entity SDK Messages
DECLARE @DisassociateEntitiesFilterId uniqueidentifier
DECLARE @AssociateEntitiesFilterId uniqueidentifier
-- Dane. Changed SdkMessageFilterId to SdkMessageId
SET @DisassociateEntitiesFilterId = (SELECT SdkMessageId FROM SdkMessageBase WHERE [Name] = 'DisassociateEntities')
-- Dane. Changed SdkMessageFilterId to SdkMessageId
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
-- Dane. Changed SdkMessageFilterId to SdkMessageId
UPDATE SdkMessageFilterBase SET IsCustomProcessingStepAllowed = 1
WHERE SdkMessageId = @DisassociateEntitiesFilterId AND PrimaryObjectTypeCode = 0

-- Dane. Changed SdkMessageFilterId to SdkMessageId
UPDATE SdkMessageFilterBase SET IsCustomProcessingStepAllowed = 1
WHERE SdkMessageId = @AssociateEntitiesFilterId AND PrimaryObjectTypeCode = 0
Thanks and fixed
If you set the Expand attribute on the SdkMessageBase for the AssocaitedEntities message it will also fire when you programatically use the AssociateEntitiesRequest class.

UPDATE SdkMessageBase SET Expand = 1
WHERE SdkMessageId = @AssociateEntitiesFilterId
Add a New Comment
Name

Email Address

Url

Comment