Wednesday, August 3, 2011

Creating Many to Many Records in CRM 2011(N:N) using C#


There is a bit of change in CRM 2011 than CRM 4. The code is below.


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;
            }

7 comments:

  1. Hi Charith,

    The class AssociateEntitiesRequest in your example is Deprecated according to Msdn:

    AssociateEntitiesRequest Class. Deprecated. Use AssociateRequest.
    Namespace: Microsoft.Crm.Sdk.Messages
    Assembly: Microsoft.Crm.Sdk.Proxy (in microsoft.crm.sdk.proxy.dll)

    Source: http://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.associateentitiesrequest.aspx

    Kind regards,

    PPerry

    ReplyDelete
  2. Thanks PPerry for showing this out..
    Will Try to change the code and re publish again..

    Regards,
    Charith

    ReplyDelete
  3. getting error when we use this code. pls help me

    ReplyDelete
  4. Used AssociateRequest as mentioned in PPerry's post. Worked for me. Sample code:

    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);

    ReplyDelete
  5. Hello,

    Thanks for updating the code above. I will change the code in the post within this week.

    Charith.

    ReplyDelete
  6. Thanks Charith. This code helped me a lot today :)

    Ashanth

    ReplyDelete
  7. Hi,

    I'd like to modify "clone" message for product entity to clone associated many to many records, out of box it only clones the product record and not the relationships, can you help with this please?

    Thanks,
    Mary

    ReplyDelete

Retrieving Calendar of a Bookable Resource in Dynamics

There are occasions where we need to retrieve working days and working times of a resource in Dynamics grammatically. This is quite possible...