Compartir a través de


CreateManyToManyRequest Class

Definition

Contains the data that is needed to create a new Many-to-Many (N:N) table relationship.

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

Examples

The following example shows how to use this message. For this sample to work correctly, you must be connected to the server to get an IOrganizationService interface instance. This example also demonstrates how to validate that both tables are eligible to participate in an N:N relationship by using the CanManyToManyRequest class;

/// <summary>
/// Demonstrates creating many-to-many relationship
/// </summary>
/// <param name="service">The authenticated IOrganizationService instance</param>
static void CreateManyToManyExample(IOrganizationService service) {

    // Solution and language information
    string solutionUniqueName = "SolutionName";
    string prefix = "sample";
    int languageCode = 1033; //English

    // Information for First Table
    var tableOneSchemaName = "Account";
    var tableOneLogicalName = tableOneSchemaName.ToLower();
    var tableOneCollectionSchemaName = "Accounts";
    Label tableOneLabel = new(tableOneCollectionSchemaName, languageCode);

    // Information for Second Table
    var tableTwoSchemaName = "sample_BankAccount";
    var tableTwoLogicalName = tableTwoSchemaName.ToLower();
    var tableTwoCollectionSchemaName = "sample_BankAccounts";
    Label tableTwoLabel = new("Bank Accounts", languageCode);


    // Verify both tables are eligible;
    bool isTableOneEligible = IsEligible(tableOneLogicalName);
    bool isTableTwoElegible = IsEligible(tableTwoLogicalName);

    if (isTableOneEligible && isTableTwoElegible)
    {
        // Create the request
        CreateManyToManyRequest request = new() {
            IntersectEntitySchemaName = $"{prefix}_" +
                    $"{tableOneCollectionSchemaName}_" +
                    $"{tableTwoCollectionSchemaName}",
            ManyToManyRelationship = 
                new ManyToManyRelationshipMetadata() { 
                    SchemaName = $"{prefix}_" +
                        $"{tableOneCollectionSchemaName}_" +
                        $"{tableTwoCollectionSchemaName}",
                    Entity1LogicalName = tableOneLogicalName,
                    Entity1AssociatedMenuConfiguration = 
                        new AssociatedMenuConfiguration()
                        {
                                Behavior = AssociatedMenuBehavior.UseLabel,
                                Group = AssociatedMenuGroup.Details,
                                Label = tableOneLabel,
                                Order = 10000

                        },
                    Entity2LogicalName = tableTwoLogicalName,
                    Entity2AssociatedMenuConfiguration = 
                        new AssociatedMenuConfiguration()
                        {
                            Behavior = AssociatedMenuBehavior.UseLabel,
                            Group = AssociatedMenuGroup.Details,
                            Label = tableTwoLabel,
                            Order = 10000
                        }
            },
            SolutionUniqueName = solutionUniqueName

        };

        // Send the request
        var response = (CreateManyToManyResponse)service.Execute(request);

        // Output relationship ID
        Console.WriteLine($"ManyToManyRelationshipId: {response.ManyToManyRelationshipId}");

    }
    else {
        Console.WriteLine("One of these tables can't be in an N:N relationship");
    }

    // Validates that the table can participate in an N:N relationship
    bool IsEligible(string tableLogicalName) {

        CanManyToManyRequest request = new() { 
            EntityName = tableLogicalName
        };

        var response = (CanManyToManyResponse)service.Execute(request);
        return response.CanManyToMany;       
    }
}

Sample code on GitHub

Create and retrieve table relationships

Remarks

Usage

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

Privileges and Access Rights

To perform this action, the caller must have the following privileges that are assigned to the System Administrator and System Customizer roles:

prvReadQuery

Notes for Callers

Constructors

CreateManyToManyRequest()

Initializes a new instance of the CreateManyToManyRequest class.

Properties

ExtensionData

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

(Inherited from OrganizationRequest)
IntersectEntitySchemaName

Gets or sets the name of the intersect entity to be created for this entity relationship. Required.

Item[String]

Gets or sets the indexer for the Parameters collection.

(Inherited from OrganizationRequest)
ManyToManyRelationship

Gets or sets the definition of the Many-to-Many table relationship to be created. Required.

Parameters

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

(Inherited from OrganizationRequest)
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)
SolutionUniqueName

Gets or sets the name of the unmanaged solution you want to add this many-to-many entity relationship to. Optional.

Applies to

See also