EvilDevilCuckoo, Evil Devil Cuckoo, William Ryan, W.G. Ryan, Bill Ryan, Kim Ryan, Search Engine Optimization, Data Mining
AssociateEntitiesRequest is an implementation of the Request/Response pattern used throughout Microsoft Dynamics CRM 2011. Any time you have entities related in a N-N fashion, you can use this request to programmatically bind them together. [This sample uses C# but can easily be ported to use any other .NET language including Visual Basic .NET]
In order to use this request, you’re going to have to add a reference to the Microsoft.Crm.Sdk.Proxy.dll library (which is located in the bin directory of the SDK). This request is a member of the Microsoft.Crm.Sdk.Messages namespace. Hence the fully qualified type name is Microsoft.Crm.Sdk.Messages.AssociateEntitiesRequest. Because there is no Microsoft.Crm.Sdk.Messages dll, this is the source of confusion for many newcomers. You’ll still likely need a reference to Microsoft.Xrm.Sdk, Microsoft.Xrm.Client (if you want to use the CrmConnection), System.Runtime.Serialization and System.ServiceModel if you want to use the OrganizationService and System.Configuration if you want to reference a value in the .config file.
Conceptually, think of a Contract. A Contract may have Terms & Conditions. Terms & Conditions are generally applicable to states or jurisdictions. I’m not a lawyer so if I’m talking out of my a55, please ignore it, it’s just an example. Assume you have a Contract entity, a T&C Entity and a State entity. You want to associate a set of T&Cs with a state. Just to make things easy, I’m going to declare two properties, one a CrmConnection and the other of type IOrganizationService. The CrmConnection points to a connectionstring named CrmMain added in a config file .(I add the DebuggerStepThrough attribute on the Get accessors just for good measure b/c stepping through the accessor repeatedly can be a huge waste of time
public static IOrganizationService ServiceInstance
if (serviceInstance == null)
serviceInstance = new OrganizationService(ConnectionInstance);
public static CrmConnection ConnectionInstance
if (connectionInstance == null)
connectionInstance = new CrmConnection("CrmMain");
With that in place, we’re ready to use the AssociateEntitiesRequest. You’ll need to know three things.
If you have the names for Moniker1, Moniker2 and RelationshipName, ignore the next paragraph.
If you do not know any of the three items mentioned above, the following will help you identify them:
To find the name of the relationship. open the solution, find either of the Entities in question and choose the N:N Relationship node. You’ll see a list of possible relationships on the right hand side. There shouldn’t be too many of these generally and it should strike out at you (b/c the “Other Entity” column should have the name of one of the two entities you’re looking to relate);
With these items identified, the associating the items is absolutely trivial.
1- Declare and instantiate an instance of the AssociateEntitiesRequest. It only has the default constructor available so there’s nothing exotic about it:
AssociateEntitiesRequest AssociateRequest = new AssociateEntitiesRequest();
2- You need to set the Moniker1 and Moniker2 properties of the request. These will be of type EntityReference. To create an EntityReference, you simply need the entity’s identifier (the Guid assigned by CRM) and the CRM type name. So for example, if you wanted to create an EntityReference to an Account entity with a Guid of 73BC8091-3B13-452A-AC0E-BE5EB18BF735, you’d do the following:
EntityReference AccountReference = new EntityReference("account", new Guid("73BC8091-3B13-452A-AC0E-BE5EB18BF735");
You’ll do the same for the Moniker1 and Moniker2 properties.
AssociateRequest.Moniker1 = new EntityReference("dev_terms", termId);
AssociateRequest.Moniker2 = new EntityReference("dev_state", stateId);
3- Set the RelationshipName property (in this case, “dev_terms_state“).
AssociateRequest.RelationshipName = "dev_terms_state";
4- Call the Execute methodsof the OrganizationService instance which we created at the beginning of the article. Pass in the AssociateEntitiesRequest instance we just created.
I’ve left off correct exception handling and logging for the sake of readability, but it should go without saying that any production code should take exceptions into account. If you did this correctly and didn’t encounter any errors, you’ll now see the items you specified in CRM associated together
If you have not already done so, please make sure you have downloaded the Microsoft Dynamics CRM 2011 Software Development Kit (SDK). There are certain features only available in certain rollups, so make sure the version of the SDK you are using matches the rollup you are currently working with on your Dynamics CRM 2011 instance (or the rollup you plan to be using)
Keywords: AssociateEntitiesRequest, , Moniker1, Moniker2, RelationshipName, DebuggerStepThrough, DebuggerStepThroughAttribute, Microsoft.Crm.Sdk.Proxy, Micrososft.Crm.Sdk.Messages, EntityReference, Guid, MSCRM, Microsoft Dynamics CRM, Dynamics CRM 2011, Microsoft Dynamics CRM 2011 Software Development Kit, ConnectionString, CrmConnection.ConnectionString, Bill Ryan, William G. Ryan, OrganizationService, IOrganizationService, CrmConnection, AssociateEntitiesRequest