Jaa


CreateOneToManyRequest Class

Definition

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

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

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

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

    // Information on the table to be referenced
    string referencedTableSchemaName = "Account";
    string referencedTableLogicalName = referencedTableSchemaName.ToLower();
    string referencedTableDisplayName = "Account";

    // Information on the referencing table
    string referencingTableSchemaName = "sample_BankAccount";
    string referencingTableLogicalName = referencingTableSchemaName.ToLower();


    // Only proceed if both tables are eligible to participate
    if (IsEligibleForOneToManyRelationship(referencedTableLogicalName, referencingTableLogicalName))
    {
        CreateOneToManyRequest request = new()
        {
            // Defines the lookup to create on the Referencing table
            Lookup = new LookupAttributeMetadata
            {
                SchemaName = $"{prefix}_{referencedTableSchemaName}Id",
                DisplayName = new Label(referencedTableDisplayName, languageCode),
                RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
                Description = new Label($"Lookup to the {referencedTableDisplayName} table.", languageCode)
            },
            // Defines the relationship to create to support the lookup
            OneToManyRelationship = new OneToManyRelationshipMetadata
            {
                ReferencedEntity = referencedTableLogicalName,
                ReferencingEntity = referencingTableLogicalName,
                SchemaName = $"{prefix}_{referencedTableSchemaName}_{referencingTableSchemaName}",
                // Controls how the relationship appears in model-driven apps
                AssociatedMenuConfiguration = new AssociatedMenuConfiguration
                {
                    Behavior = AssociatedMenuBehavior.UseLabel,
                    Group = AssociatedMenuGroup.Details,
                    Label = new Label(referencedTableDisplayName, languageCode),
                    Order = 10000
                },
                // Controls automated behaviors for related records
                CascadeConfiguration = new CascadeConfiguration
                {
                    Assign = CascadeType.NoCascade,
                    Delete = CascadeType.RemoveLink,
                    Merge = CascadeType.NoCascade,
                    Reparent = CascadeType.NoCascade,
                    Share = CascadeType.NoCascade,
                    Unshare = CascadeType.NoCascade
                }
            },
            // Creates all solution components as part of this solution
            SolutionUniqueName = solutionUniqueName
        };

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

        // Output ID of the created relationship
        Console.WriteLine($"Created RelationshipId:{response.RelationshipId}");
        // Output ID of the created lookup column
        Console.WriteLine($"Created AttributeId:{response.AttributeId}");        
    }
    else
    {
        Console.WriteLine("One of the tables isn't eligible to participate in a 1:N relationship");
    }

    //Checks whether the specified table can be the primary table in one-to-many
    //relationship.
    bool IsEligibleForOneToManyRelationship(
          string referencedTableLogicalName,
          string referencingTableLogicalName)
    {

        CanBeReferencedRequest canBeReferencedRequest = new()
        {
            EntityName = referencedTableLogicalName
        };

        var canBeReferencedResponse =
            (CanBeReferencedResponse)service.Execute(canBeReferencedRequest);

        //Checks whether the specified table can be the referencing table in one-to-many
        //relationship.
        CanBeReferencingRequest canBeReferencingRequest = new()
        {
            EntityName = referencingTableLogicalName
        };

        CanBeReferencingResponse canBeReferencingResponse =
            (CanBeReferencingResponse)service.Execute(canBeReferencingRequest);

        return canBeReferencedResponse.CanBeReferenced && canBeReferencingResponse.CanBeReferencing;
    }
}

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 CreateOneToManyResponse.

Privileges and Access Rights

To perform this action, the caller must have privileges listed in CreateOneToMany message privileges.

Notes for Callers

You can use the CanBeReferencedRequest and CanBeReferencingRequest classes to verify that the tables are eligible to participate in a One-to-Many relationship.

Constructors

CreateOneToManyRequest()

Initializes a new instance of the CreateOneToManyRequest 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)
Lookup

Gets or sets the metadata for the lookup column used to store the ID of the related record. Required.

OneToManyRelationship

Gets or sets the metadata for the relationship. 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 one-to-Many entity relationship to. Optional.

Applies to

See also