Partilhar via


CreatePolymorphicLookupAttributeRequest Class

Definition

Contains the data to create a multi-table lookup column.

public ref class CreatePolymorphicLookupAttributeRequest sealed : Microsoft::Xrm::Sdk::OrganizationRequest
[System.Runtime.Serialization.DataContract(Namespace="http://schemas.microsoft.com/crm/2011/Contracts")]
public sealed class CreatePolymorphicLookupAttributeRequest : Microsoft.Xrm.Sdk.OrganizationRequest
[<System.Runtime.Serialization.DataContract(Namespace="http://schemas.microsoft.com/crm/2011/Contracts")>]
type CreatePolymorphicLookupAttributeRequest = class
    inherit OrganizationRequest
Public NotInheritable Class CreatePolymorphicLookupAttributeRequest
Inherits OrganizationRequest
Inheritance
CreatePolymorphicLookupAttributeRequest
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 static CreatePolymorphicLookupAttributeExample example method creates a multi-table lookup column named sample_BankAccountOwnerId for the sample_BankAccount table that can associate the record to an Account, Contact, or Lead record.

/// <summary>
/// Demonstrates creating a multi-table lookup column
/// </summary>
/// <param name="service">The authenticated IOrganizationService instance.</param>
static void CreatePolymorphicLookupAttributeExample(IOrganizationService service)
{
      // Solution and language information
      string solutionUniqueName = "SolutionName";
      string prefix = "sample";
      int languageCode = 1033; //English
      string referencingTableSchemaName = "sample_BankAccount";

      // The tables to make available for the lookup
      List<string> tableSchemaNames = new() { 
         "Account",
         "Contact",
         "Lead"
      };

      // A list of relationhip definitions for each table
      List<OneToManyRelationshipMetadata> oneToManyRelationships = new();

      // Create the relationships for each table
      tableSchemaNames.ForEach(tableSchemaName => {

         OneToManyRelationshipMetadata relationship = new() { 
            ReferencedEntity = tableSchemaName.ToLower(),
            ReferencingEntity = referencingTableSchemaName.ToLower(),
            SchemaName = $"{prefix}_{tableSchemaName}_{referencingTableSchemaName}",
            // Controls how the relationship appears in model-driven apps
            AssociatedMenuConfiguration = new AssociatedMenuConfiguration
            {
                  Behavior = AssociatedMenuBehavior.UseLabel,
                  Group = AssociatedMenuGroup.Details,
                  Label = new Label($"{tableSchemaName}s", 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
            }
         };

         // Add the relationship to the list
         oneToManyRelationships.Add(relationship);
      });

      // Define the lookup column
      LookupAttributeMetadata lookupColumn = new() {
         SchemaName = $"{prefix}_BankAccountOwnerId",
         DisplayName = new Label("Bank Account Owner", languageCode),
         RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
         Description = new Label(
            $"Lookup to the {string.Join(",",tableSchemaNames)} tables.", 
            languageCode)
      };

      // Prepare the request
      CreatePolymorphicLookupAttributeRequest request = new() { 
         Lookup = lookupColumn,
         OneToManyRelationships = oneToManyRelationships.ToArray(),
         SolutionUniqueName = solutionUniqueName
      };

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

      // Output the details returned:
      Console.WriteLine($"Created multi-table lookup column with ID: {response.AttributeId}");
      Console.WriteLine("Created relationships with these IDs:");
      foreach (Guid id in response.RelationshipIds)
      {
         Console.WriteLine($"\t{id}");
      }
}

The output of the example looks like this:

Created multi-table lookup column with ID: de15898d-88fe-4d82-be5b-104b846edf20
Created relationships with these IDs:
         4b240988-9697-ee11-be37-000d3a993550
         52240988-9697-ee11-be37-000d3a993550
         59240988-9697-ee11-be37-000d3a993550

Remarks

Multi-table lookup type columns allow a user to use a specific table that has multiple one-to-many (1:N) relationships to other tables in the environment. A single lookup type column can refer to multiple other tables. A lookup value submitted to the multi-table type column will be matched to a record in any of the related tables.

Usage

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

Privileges and Access Rights

To perform this action, the caller must have the system administrator or system customizer security role.

Constructors

CreatePolymorphicLookupAttributeRequest()

Initializes a new instance of the CreatePolymorphicLookupAttributeRequest 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 lookup column definition that stores a reference to the related row.

OneToManyRelationships

Gets or sets the one-to-many relationship definitions for each table that the multi-table lookup column supports.

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 to add the multi-table lookup column to.

Applies to