Service. Associate Method
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Creates a link between records.
public: void Associate(System::String ^ entityName, Guid entityId, Microsoft::Xrm::Sdk::Relationship ^ relationship, Microsoft::Xrm::Sdk::EntityReferenceCollection ^ relatedEntities);
[System.ServiceModel.FaultContract(typeof(Microsoft.Xrm.Sdk.OrganizationServiceFault))] [System.ServiceModel.OperationContract] public void Associate (string entityName, Guid entityId, Microsoft.Xrm.Sdk.Relationship relationship, Microsoft.Xrm.Sdk.EntityReferenceCollection relatedEntities);
[<System.ServiceModel.FaultContract(typeof(Microsoft.Xrm.Sdk.OrganizationServiceFault))>] [<System.ServiceModel.OperationContract>] abstract member Associate : string * Guid * Microsoft.Xrm.Sdk.Relationship * Microsoft.Xrm.Sdk.EntityReferenceCollection -> unit
Public Sub Associate (entityName As String, entityId As Guid, relationship As Relationship, relatedEntities As EntityReferenceCollection)
Type: String. The logical name of the entity that is specified in the
Type: Relationship. The name of the relationship to be used to create the link.
Type: EntityReferenceCollection. A collection of entity references (references to records) to be associated.
The following example shows how to use the Associate(String, Guid, Relationship, EntityReferenceCollection) method to create an association or link between records. For this sample to work correctly, you must be connected to the server to get an IOrganizationService interface. You can find the complete sample in the sample code package in the folder
// The account ID would typically be passed in as an argument or determined by a query. // The contact ID would typically be passed in as an argument or determined by a query. // Associate the accounts to the contact record. //Create a collection of the entity ids that will be associated to the contact. EntityReferenceCollection relatedEntities = new EntityReferenceCollection(); relatedEntities.Add(new EntityReference("account", _account1Id)); relatedEntities.Add(new EntityReference("account", _account2Id)); relatedEntities.Add(new EntityReference("account", _account3Id)); // Create an object that defines the relationship between the contact and account. Relationship relationship = new Relationship("account_primary_contact"); //Associate the contact with the 3 accounts. _service.Associate("contact", _contactId, relationship, relatedEntities); Console.WriteLine("The entities have been associated."); //Disassociate the records. _service.Disassociate("contact", _contactId, relationship, relatedEntities);
Privileges and Access Rights
To perform this action, the caller must have privileges on the entity that is specified in the
entityName parameter and access rights on the record that is specified in the
entityId parameter. For a list of the required privileges, see Delete message privileges.
Notes for Callers
This method creates multiple associations in one transaction between the record that is specified by the
entityId parameter and each record in the
relatedEntities parameter for the specified relationship in the
For a one-to-many relationship, this method sets the ReferencingAttribute in the related record to the value of the
For a many-to-many relationship, this method creates a record in the intersect table for the relationship, which contains the ID of both the referenced and referencing records. The intersect table name is defined in the IntersectEntityName property for the relationship. You need this when you query for the records. However, you need the relationship name to set the Relationship property. This name is defined in the SchemaName property for the relationship.
For more information about the exceptions that can be thrown when this method is called, see Handle exceptions in your code.
You can use this method to associate any records that participate in a relationship, including records for custom entities.