Friday, August 28, 2009

Many to Many Relationship

Many to Many relationships is not something new to CRM 4.0, they existed in CRM 3.0 as well,
however there was no ability to create new many to many relationships in the previous version, which is available in the current version.
When a Many to Many relationship is created between 2 entities, a custom table is created to maintain the relationship, which is called as the relationship table.This table stores the primary key values of both the entities.

Associating Records with a Many to Many Relationship:Assuming there are 2 entities named 'Student' and 'Book' which have a Many to Many relationship.Here's a sample code which associates multiple 'Book' entity records to the 'Student' Entity record.

Moniker Moniker1 = new Moniker();
Moniker1.Id = studentid;
Moniker1.Name = EntityName.student.ToString();
//BookList is a list which contains the guid of the 'Book' entity records.
for (int x = 0; x < BookList.Count; x++)
// Create a request.
AssociateEntitiesRequest request = new AssociateEntitiesRequest();
// Assign the request a moniker for both entities that need to be associated.
Moniker Moniker2 = new Moniker();

Moniker2.Id = new Guid(BookList[x][0].ToString());
Moniker2.Name =;

request.Moniker1 = Moniker1;
request.Moniker2 = Moniker2;
// Set the relationship name that associates the two entities.
//Refer relationship properties in CRMrequest.
RelationshipName = [Enter the name of the Relationship here];
// Execute the request.
AssociateEntitiesResponse response = (AssociateEntitiesResponse)myCrm.Execute(request);

Retrieving Associated Many to Many Relationships records:Note : RetriveMultiple method cannot be used to retrieve the records associated with a Many to Many relationship.
The sample code below shows how to retrieve records having a Many to Many Relationships using the LinkEntity class.Here we retrive the Books associated with a student record.

// Create a query expression.
QueryExpression qe = new QueryExpression();
qe.EntityName =;
qe.ColumnSet = colSet;
// Create the link entity from book to student
LinkEntity leToRetrieve = new LinkEntity();
leToRetrieve.LinkFromEntityName =;
leToRetrieve.LinkFromAttributeName = "bookid";
leToRetrieve.LinkToEntityName = [Enter the name of the Relationship table here];
leToRetrieve.LinkToAttributeName = "bookid";
LinkEntity leASFilter = new LinkEntity();
leASFilter.LinkFromEntityName = [Enter the name of the Relationship table here];
leASFilter.LinkFromAttributeName = "studentid";
leASFilter.LinkToEntityName = EntityName.student.toString();
leASFilter.LinkToAttributeName = "studentid";
// Create the condition to test the user ID.
ConditionExpression cExpression = new ConditionExpression();
cExpression.AttributeName = "studentid";
cExpression.Operator = ConditionOperator.Equal;
cExpression.Values = new object[] { [STUDENT GUID] };
// Add the condition to the link entity.
leASFilter.LinkCriteria = new FilterExpression();
leASFilter.LinkCriteria.Conditions = new ConditionExpression[] { cExpression };
// Add the from and to links to the query.
leToRetrieve.LinkEntities = new LinkEntity[] { leASFilter };
qe.LinkEntities = new LinkEntity[] { leToRetrieve };
RetrieveMultipleRequest request = new RetrieveMultipleRequest();
request.Query = qe;
request.ReturnDynamicEntities = true;
RetrieveMultipleResponse response = (RetrieveMultipleResponse)crmService.Execute(request);
bec = response.BusinessEntityCollection.BusinessEntities;

Refer the below link for an fetchXml solution by Ranjit Raghuwanshi.

No comments:

Post a Comment