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 = {
                                referencedEntity
                            }
                        };

                        referrerEntity.RelatedEntities.Add(rs, referencedEntity);
                        _serviceProxy.Update(referrerEntity);


or
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))
                            {
                                tour.RelatedEntities[rs].Entities.Add(referencedEntity);
                                context.UpdateObject(referrerEntity);
                                context.SaveChanges();
                            }
                        }

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



regards,

Comments

  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

    ReplyDelete

Post a Comment

Popular posts from this blog

Complex Query in QueryExpression in Microsoft CRM 2011

Exception caught instantiating TERADATA report server extension SQL Reporting Services

Microsoft Power Apps Portal integration with Dynamics 365 CE On-Premise - Step By Step Guide