make relationship in Microsoft CRM SDK

If you want to make 1 to N relation between two entities in CRM.
You have couple ways.I would like to share two of them.

First of all;
You can make relation after creating new record .

                        CustomEntityInCRM1 referrerEntity = ...//retrieve data;
                        CustomEntityInCRM2 referencedEntity = ...//retrieve data;

                        Relationship rs = new Relationship("");
                        EntityCollection relatedEntities = new EntityCollection
                            EntityName = referencedEntity.LogicalName,
                            Entities = {

                        referrerEntity.RelatedEntities.Add(rs, referencedEntity);

you can retrieve two of them and make connection (prevent 'The collection is read-only error')

                        Relationship rs = new Relationship(""); 
                        context.LoadProperty(tour, "");

                        CustomEntityInCRM1 referrerEntity = ...//retrieve data;
                        CustomEntityInCRM2 referencedEntity = ...//retrieve data;

                        if (referrerEntity.RelatedEntities.Count > 0)
                            DataCollection relationcoll = referrerEntity.RelatedEntities[rs].Entities;
                            if (!relationcoll.Contains(con))

What about N to N;

                        // Create an AssociateEntities request.
                        AssociateEntitiesRequest request = new AssociateEntitiesRequest();

                        Microsoft.Xrm.Sdk.EntityReference Moniker1 = new Microsoft.Xrm.Sdk.EntityReference();
                        Moniker1.Id = New_CustomEntity1.Id;
                        Moniker1.Name = entity1.LogicalName;//Entity Name

                        // Code Create Moniker for second Entity: New_CustomEntity2
                        Microsoft.Xrm.Sdk.EntityReference Moniker2 = new Microsoft.Xrm.Sdk.EntityReference();
                        Moniker2.Id = New_CustomEntity2.Id;
                        Moniker2.Name = New_CustomEntity2.LogicalName;//Entity Name

                        // Set the ID of Moniker1 to the ID of the Moniker2.
                        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 = "oti_oti_ticket_contact";

                        // Execute the request.
                        _serviceProxy.Execute(request); //OrganizationServiceProxy _serviceProxy



  1. Not sure whether it was relevant when you made your posting, but the method I use now is the AddLink extension method on the generated context


Post a comment

Popular posts from this blog

Assembly Microsoft.Dynamics.Service.Plugins.dll can not be loaded. Dynamics CRM 365 Engine version 9 - CRM User creation error

Exception caught instantiating TERADATA report server extension SQL Reporting Services

Could not load file or assembly 'System.ServiceModel, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified at Configuration class initiation in