Compartilhar via


IOrganizationService.Associate Method

Definition

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)

Parameters

entityName
String

The logical name of the table for the record that is specified in the entityId parameter.

entityId
Guid

The ID of the record to which the related records are associated.

relationship
Relationship

The name of the relationship to be used to create the link.

relatedEntities
EntityReferenceCollection

A collection of entity references (references to records) to be associated.

Attributes

Examples

The following example method 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 instance.

/// <summary>
/// Demonstrates the Associate method
/// </summary>
/// <param name="service">Authenticated IOrganizationService instance</param>
/// <param name="target">The retrieved record to relate the other records to</param>
/// <param name="recordsToRelate">The retrieved records to relate to the target record</param>
/// <param name="relationshipName">The name of the relationship</param>
static void AssociateMethodExample(
      IOrganizationService service, 
      Entity target, 
      EntityCollection recordsToRelate, 
      string relationshipName) {

      EntityReferenceCollection entityReferences = new();

      foreach (Entity record in recordsToRelate.Entities)
      {
         entityReferences.Add(record.ToEntityReference());
      }

      service.Associate(
         entityName: target.LogicalName,
         entityId: target.Id,
         relationship: new Relationship(relationshipName),
         relatedEntities: entityReferences);
}

You can find more samples on GitHub:

Remarks

Learn more about how to associate and disassociate table rows using the SDK for .NET.

Privileges and Access Rights

To perform this action, the caller must have:

  • Read, Write, and AppendTo privileges for the table specified by the entityName parameter and the same access rights for the record specified by entityId parameter.

  • Read, Write, and Append privileges for the related table specified by the relationship parameter and the same access rights for the records specified by relatedEntities parameter.

Learn more about how to verify access with code.

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 relationship parameter.

For a one-to-many relationship, this method sets the lookup column specified by the OneToManyRelationshipMetadata.ReferencingAttribute in each of the related records to the value of the entityId.

For a many-to-many relationship, this method creates one or more records 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 ManyToManyRelationshipMetadata.IntersectEntityName property for the relationship. You need the intersect table name when you query for the records. However, you only need the relationship name to set the relationship parameter. This name is defined in the ManyToManyRelationshipMetadata.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.

Supported tables

You can use this method to associate any records that participate in a relationship. The Associate message isn't bound to a table.

Applies to

See also