Megosztás a következőn keresztül:


AssociateRequest Class

Definition

Contains the data that is needed to related one or more records.

public ref class AssociateRequest sealed : Microsoft::Xrm::Sdk::OrganizationRequest
[System.Runtime.Serialization.DataContract(Namespace="http://schemas.microsoft.com/xrm/2011/Contracts")]
public sealed class AssociateRequest : Microsoft.Xrm.Sdk.OrganizationRequest
[<System.Runtime.Serialization.DataContract(Namespace="http://schemas.microsoft.com/xrm/2011/Contracts")>]
type AssociateRequest = class
    inherit OrganizationRequest
Public NotInheritable Class AssociateRequest
Inherits OrganizationRequest
Inheritance
AssociateRequest
Attributes

Examples

The following example shows how to use this message. For this sample to work correctly, you must have an authenticated connection to the server with a client that implements the IOrganizationService interface instance.

/// <summary>
/// Demonstrates the AssociateRequest class
/// </summary>
/// <param name="service">Authenticated IOrganizationService instance</param>
/// <param name="primaryRecordReference">Reference to the record to append the records to.</param>
/// <param name="relatedRecordReferences">References to the records to append to the record.</param>
/// <param name="relationshipName">The SchemaName of the relationship</param>
static void AssociateRequestExample(IOrganizationService service,
      EntityReference primaryRecordReference,
      EntityReferenceCollection relatedRecordReferences,
      string relationshipName)
{

      AssociateRequest request = new()
      {
         RelatedEntities = relatedRecordReferences,
         Relationship = new Relationship(relationshipName),
         Target = primaryRecordReference
      };

      service.Execute(request);

      // The Associate method achieves the same result:

      //service.Associate(
      //    entityName: primaryRecordReference.LogicalName, 
      //    entityId: primaryRecordReference.Id, 
      //    relationship: new Relationship(relationshipName), 
      //    relatedEntities: relatedRecordReferences);
}

/// <summary>
/// Demonstrates the AssociateRequestExample method with a one-to-many relationship
/// </summary>
/// <param name="service">Authenticated IOrganizationService instance</param>
static void AssociateRequestOneToManyExample(IOrganizationService service)
{
      // Create account record
      Entity account = new("account")
      {
         Attributes = {
            {"name","Test Account" }
         }
      };
      Guid accountId = service.Create(account);

      // Create task records
      Entity task1 = new("task")
      {
         Attributes = {
            {"subject","Test Task 1" }
         }
      };
      // Create first task
      Guid task1Id = service.Create(task1);
      EntityReference task1Reference = new("task", task1Id);

      Entity task2 = new("task")
      {
         Attributes = {
            {"subject","Test Task 2" }
         }
      };
      // Create second task
      Guid task2Id = service.Create(task2);
      EntityReference task2Reference = new("task", task2Id);

      List<EntityReference> taskReferenceList = new() { 
         task1Reference, 
         task2Reference 
      };

      // Create account reference
      EntityReference accountRecordReference = new("account", accountId);
      // Create task references
      EntityReferenceCollection taskReferences = new(taskReferenceList);

      // Test the AssociateRequestExample method:
      AssociateRequestExample(
         service: service, 
         primaryRecordReference: accountRecordReference, 
         relatedRecordReferences: taskReferences,
         relationshipName:"Account_Tasks"
         );

      // Retrieve account and related tasks with one request
      RelationshipQueryCollection relationshipQueryCollection = new();

      QueryExpression relatedTasks = new("task")
      {
         ColumnSet = new ColumnSet("subject")
      };
      Relationship taskRelationship = new("Account_Tasks");
      relationshipQueryCollection.Add(taskRelationship, relatedTasks);

      RetrieveRequest request = new()
      {
         ColumnSet = new ColumnSet("name"),
         RelatedEntitiesQuery = relationshipQueryCollection,
         Target = new EntityReference("account", accountId)
      };

      var response = (RetrieveResponse)service.Execute(request);

      Entity retrievedAccount = response.Entity;

      Console.WriteLine("Account Name: {0}", retrievedAccount["name"]);

      var tasks = retrievedAccount.RelatedEntities[new Relationship("Account_Tasks")];

      Console.WriteLine("Tasks:");
      tasks.Entities.ToList().ForEach(x => {
         Console.WriteLine(" Task Subject: {0}", x["subject"]);
      });


      // Delete the account record
      service.Delete("account", accountId);
      // Tasks will be automatically deleted due to cascading relationship
}

Example output of the AssociateRequestOneToManyExample method showing the related task records retrieved with the account record:

Account Name: Test Account
Tasks:
   Task Subject: Test Task 1
   Task Subject: Test Task 2

Learn more about retrieving a record with related rows.

Sample code on GitHub

Associate and disassociate table rows

Remarks

Usage

Pass an instance of this class to the Execute(OrganizationRequest) method, which returns an instance of the AssociateResponse class.

Privileges and Access Rights

To perform this action, the caller must have:

  • Read, Write, and AppendTo privileges and access rights for the table and record specified by the Target property.

  • Read, Write, and Append privileges and access rights for the tables and records specified by the RelatedEntities property.

Notes for Callers

This message uses a single transaction to create multiple associations between the record that is specified by Target property and each record specified in the RelatedEntities property. The associations are created for the specified relationship in the Relationship property.

You can also use the Associate(String, Guid, Relationship, EntityReferenceCollection) method to perform the same operation.

For a one-to-many relationship, this message sets the lookup column specified by the OneToManyRelationshipMetadata.ReferencingAttribute property in the related record to the value of the Id in the Target.

For a many-to-many relationship, this message 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 ManyToManyRelationshipMetadata.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 ManyToManyRelationshipMetadata.SchemaName property.

Supported Tables

You can use this message to associate records of any two tables that participate in a relationship. To find the default relationships for common tables, see the Dataverse table/entity reference article for the table.

To find relationships for custom tables or custom relationships, use Power Apps to view relationship definitions for the table, or use a table definition browser like the one described in Browse table definitions in your environment.

Constructors

AssociateRequest()

Initializes a new instance of the AssociateRequest class.

Properties

ExtensionData

Gets or sets the structure that contains extra data. Optional.

(Inherited from OrganizationRequest)
Item[String]

Gets or sets the indexer for the Parameters collection.

(Inherited from OrganizationRequest)
Parameters

Gets or sets the collection of parameters for the request. Required, but is supplied by derived classes.

(Inherited from OrganizationRequest)
RelatedEntities

Gets or sets the collection of entity references (references to records) to be associated. Required.

Relationship

Get or sets the relationship name to be used for an association. Required.

RequestId

Gets or sets the ID of the request. Optional.

(Inherited from OrganizationRequest)
RequestName

Gets or sets the name of the request. Required, but is supplied by derived classes.

(Inherited from OrganizationRequest)
Target

Gets or sets the target that is the record to which the related records are associated. Required.

Applies to

See also