{ADVANCED DEVELOPMENT}Associate N:N records using C# in Dynamics CRM 2013/2015: Which is the best way?

Recently I referenced a code to link n:n relationships in a project.

If you have been using the following way to associate N:N(many:many relationships) in C#, there needs to be some consideration:

Microsoft.Xrm.Sdk.EntityReference Moniker1 = new Microsoft.Xrm.Sdk.EntityReference();

Moniker1.Id = ProductID;

Moniker1.Name = “product”;//Entity Name

// Code Create Moniker for second Entity: New_CustomEntity

Microsoft.Xrm.Sdk.EntityReference Moniker2 = new Microsoft.Xrm.Sdk.EntityReference();

Moniker2.Id = contactid;

Moniker2.Name = “contact”;//Entity Name

AssociateManyToManyEntityRecords(Moniker1, Moniker2, “product_contact”);

public bool AssociateManyToManyEntityRecords(Microsoft.Xrm.Sdk.EntityReference moniker1, Microsoft.Xrm.Sdk.EntityReference moniker2, string strEntityRelationshipName)

{

try

{

// Create an AssociateEntities request.

//Namespace is Microsoft.Crm.Sdk.Messages

AssociateEntitiesRequest request = new AssociateEntitiesRequest();

// Set the ID of Moniker1 to the ID of the lead.

request.Moniker1 = new EntityReference { Id = moniker1.Id, LogicalName = moniker1.Name };

// Set the ID of Moniker2 to the ID of the contact.

request.Moniker2 = new EntityReference { Id = moniker2.Id, LogicalName = moniker2.Name };

// Set the relationship name to associate on.

request.RelationshipName = strEntityRelationshipName;

// Execute the request.

this.Service.Execute(request);

return true;

}

There is a word of caution to note here. As per official msdn link this is deprecated: http://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.associateentitiesrequest(v=crm.7).aspx

Applies To: CRM 2015 on-prem, CRM Online

Deprecated. Use the AssociateRequest class. Contains the data that is needed to add a link between two entity instances in a many-to-many relationship.

Namespace: Microsoft.Crm.Sdk.Messages
Assembly: Microsoft.Crm.Sdk.Proxy (in Microsoft.Crm.Sdk.Proxy.dll)

So, which way to go then?

Well, this is the most suited way to do this:

AssociateRequest request = new AssociateRequest();

EntityReference mon1 = new EntityReference(targetEntity.LogicalName, targetEntity.Id);

EntityReference mon2 = new EntityReference(linkEntity.LogicalName, linkEntity.Id);

request.Target = mon1;

request.RelatedEntities = new EntityReferenceCollection { mon2 };

request.Relationship = new Relationship(“relationshipName”);

context.Execute(request);

No doubt the first approach might be working for you, but depending on deprecated method is not advisable.

Hope it helps!

Trying to Learn Dynamics CRM: Download!

Learn Dynamics CRM App on Google Play store

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s