Udostępnij za pośrednictwem


IOrganizationService.Disassociate Method

Definition

Deletes a link between records.

public:
 void Disassociate(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 Disassociate (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 Disassociate : string * Guid * Microsoft.Xrm.Sdk.Relationship * Microsoft.Xrm.Sdk.EntityReferenceCollection -> unit
Public Sub Disassociate (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 from which the related records are disassociated.

relationship
Relationship

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

relatedEntities
EntityReferenceCollection

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

Attributes

Examples

The following example demonstrates how to use the Disassociate(String, Guid, Relationship, EntityReferenceCollection) method to remove an association or link between records.

/// <summary>
/// Demonstrates the Disassociate method
/// </summary>
/// <param name="service">Authenticated IOrganizationService instance</param>
/// <param name="target">The retrieved record to disassociate from the other records</param>
/// <param name="recordsToDisassociate">The retrieved records to disassociate from the target record</param>
/// <param name="relationshipName">The name of the relationship</param>
static void DisassociateExample(
      IOrganizationService service, 
      Entity target, 
      EntityCollection recordsToDisassociate, 
      string relationshipName) {

      EntityReferenceCollection entityReferences = new();

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

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

You can find more samples on GitHub:

Remarks

Learn how to disassociate table rows using the SDK for .NET.

Privileges and Access Rights

To perform this action, the caller must have Read privileges on the table that is specified in the entityName parameter and Read access rights on the record that are specified in the entityId. Callers must also have Read and Write privileges and access rights for the records referenced in the relatedEntities parameter. Learn more about how to verify access with code.

Notes for Callers

This method removes multiple associations in one transaction between the record that is specified by the entityId parameter and each record in the relatedEntities parameter for the relationship that is specified in the relationship parameter.

For a one-to-many relationship, this method clears the value of the lookup column named as the OneToManyRelationshipMetadata.ReferencingAttribute in the relationship definition.

For a many-to-many relationship, this method deletes 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 by the ManyToManyRelationshipMetadata.IntersectEntityName property in the relationship definition.

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 in the relationship definition.

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 disassociate any records that participate in a relationship. The Disassociate message isn't bound to a table.

Applies to

See also