Бөлісу құралы:


CreateAttributeRequest Class

Definition

Contains the data that is needed to create a new column, and optionally, to add it to a specified unmanaged solution.

public ref class CreateAttributeRequest sealed : Microsoft::Xrm::Sdk::OrganizationRequest
[System.Runtime.Serialization.DataContract(Namespace="http://schemas.microsoft.com/xrm/2011/Contracts")]
public sealed class CreateAttributeRequest : Microsoft.Xrm.Sdk.OrganizationRequest
[<System.Runtime.Serialization.DataContract(Namespace="http://schemas.microsoft.com/xrm/2011/Contracts")>]
type CreateAttributeRequest = class
    inherit OrganizationRequest
Public NotInheritable Class CreateAttributeRequest
Inherits OrganizationRequest
Inheritance
CreateAttributeRequest
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 code defines the AttributeMetadata for a number of different types of attributes and adds them to a List<AttributeMetadata>. At the end of the code, the CreateAttributeRequest class prepares the request, and the column is created by using the Execute(OrganizationRequest) method.

/// <summary>
/// Creates several different types of columns to the Contact table.
/// </summary>
/// <param name="service">Authenticated IOrganizationService instance</param>
static void CreateColumnsExample(IOrganizationService service)
{

      int languageCode = 1033; //English
      string prefix = "sample"; // Solution publisher customization prefix

      // Create storage for new attributes being created
      var addedAttributes = new List<AttributeMetadata>();

      // Create a boolean column
      BooleanAttributeMetadata boolAttribute = new()
      {
         // Set base properties
         SchemaName = $"{prefix}_Boolean",
         LogicalName = $"{prefix}_boolean",
         DisplayName = new Label("Sample Boolean", languageCode),
         RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
         Description = new Label("Boolean Attribute", languageCode),
         // Set extended properties
         OptionSet = new BooleanOptionSetMetadata(
            new OptionMetadata(new Label("True", languageCode), 1),
            new OptionMetadata(new Label("False", languageCode), 0)
            )
      };

      // Add to list
      addedAttributes.Add(boolAttribute);

      // Create a date time column
      DateTimeAttributeMetadata dtAttribute = new()
      {
         // Set base properties
         SchemaName = $"{prefix}_Datetime",
         LogicalName = $"{prefix}_datetime",
         DisplayName = new Label("Sample DateTime", languageCode),
         RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
         Description = new Label("DateTime Attribute", languageCode),
         // Set extended properties
         Format = Microsoft.Xrm.Sdk.Metadata.DateTimeFormat.DateOnly,
         ImeMode = ImeMode.Disabled
      };

      // Add to list
      addedAttributes.Add(dtAttribute);

      // Create a decimal column	
      DecimalAttributeMetadata decimalAttribute = new()
      {
         // Set base properties
         SchemaName = $"{prefix}_Decimal",
         LogicalName = $"{prefix}_decimal",
         DisplayName = new Label("Sample Decimal", languageCode),
         RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
         Description = new Label("Decimal Attribute", languageCode),
         // Set extended properties
         MaxValue = 100,
         MinValue = 0,
         Precision = 1
      };

      // Add to list
      addedAttributes.Add(decimalAttribute);

      // Create a integer column	
      IntegerAttributeMetadata integerAttribute = new()
      {
         // Set base properties
         SchemaName = $"{prefix}_Integer",
         LogicalName = $"{prefix}_integer",
         DisplayName = new Label("Sample Integer", languageCode),
         RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
         Description = new Label("Integer Attribute", languageCode),
         // Set extended properties
         Format = IntegerFormat.None,
         MaxValue = 100,
         MinValue = 0
      };

      // Add to list
      addedAttributes.Add(integerAttribute);

      // Create a memo column 
      MemoAttributeMetadata memoAttribute = new()
      {
         // Set base properties
         SchemaName = $"{prefix}_Memo",
         LogicalName = $"{prefix}_memo",
         DisplayName = new Label("Sample Memo", languageCode),
         RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
         Description = new Label("Memo Attribute", languageCode),
         // Set extended properties
         Format = StringFormat.TextArea,
         ImeMode = ImeMode.Disabled,
         MaxLength = 500
      };

      // Add to list
      addedAttributes.Add(memoAttribute);

      // Create a money column	
      MoneyAttributeMetadata moneyAttribute = new()
      {
         // Set base properties
         SchemaName = $"{prefix}_Money",
         LogicalName = $"{prefix}_money",
         DisplayName = new Label("Money Picklist", languageCode),
         RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
         Description = new Label("Money Attribue", languageCode),
         // Set extended properties
         MaxValue = 1000.00,
         MinValue = 0.00,
         Precision = 1,
         PrecisionSource = 1,
         ImeMode = ImeMode.Disabled
      };

      // Add to list
      addedAttributes.Add(moneyAttribute);

      // Create a choice column	
      PicklistAttributeMetadata pickListAttribute =
         new()
         {
            // Set base properties
            SchemaName = $"{prefix}_Picklist",
            LogicalName = $"{prefix}_picklist",
            DisplayName = new Label("Sample Picklist", languageCode),
            RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
            Description = new Label("Picklist Attribute", languageCode),
            // Set extended properties
            // Build local picklist options
            OptionSet = new OptionSetMetadata
            {
                  IsGlobal = false,
                  OptionSetType = OptionSetType.Picklist,
                  Options =
                  {
                     new OptionMetadata(
                        new Label("Created", languageCode), null),
                     new OptionMetadata(
                        new Label("Updated", languageCode), null),
                     new OptionMetadata(
                        new Label("Deleted", languageCode), null)
                  }
            }
         };

      // Add to list
      addedAttributes.Add(pickListAttribute);

      // Create a string column
      StringAttributeMetadata stringAttribute = new StringAttributeMetadata
      {
         // Set base properties
         SchemaName = $"{prefix}_String",
         LogicalName = $"{prefix}_string",

         DisplayName = new Label("Sample String", languageCode),
         RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
         Description = new Label("String Attribute", languageCode),
         // Set extended properties
         MaxLength = 100
      };

      // Add to list
      addedAttributes.Add(stringAttribute);


      // Create a multi-select optionset
      MultiSelectPicklistAttributeMetadata multiSelectOptionSetAttribute = new MultiSelectPicklistAttributeMetadata()
      {
         SchemaName = $"{prefix}_MultiSelectOptionSet",
         LogicalName = $"{prefix}_multiselectoptionset",
         DisplayName = new Label("Multi-Select OptionSet", languageCode),
         RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
         Description = new Label("Multi-Select OptionSet description", languageCode),
         OptionSet = new OptionSetMetadata()
         {
            IsGlobal = false,
            OptionSetType = OptionSetType.Picklist,
            Options = {
                  new OptionMetadata(new Label("First Option",languageCode),null),
                  new OptionMetadata(new Label("Second Option",languageCode),null),
                  new OptionMetadata(new Label("Third Option",languageCode),null)
                  }
         }
      };
      // Add to list
      addedAttributes.Add(multiSelectOptionSetAttribute);


      // LookupAttributeMetadata cannot be created outside the context of a relationship.
      // StateAttributeMetadata and StatusAttributeMetadata cannot be created via the SDK.

      foreach (AttributeMetadata columnDefinition in addedAttributes)
      {
         // Create the request.
         CreateAttributeRequest request = new()
         {
            EntityName = Contact.EntityLogicalName,
            Attribute = columnDefinition
         };

         // Execute the request.
         service.Execute(request);

         Console.WriteLine($"Created the {columnDefinition.SchemaName} column.");
      }
}

Output:

Created the sample_Boolean column.
Created the sample_Datetime column.
Created the sample_Decimal column.
Created the sample_Integer column.
Created the sample_Memo column.
Created the sample_Money column.
Created the sample_Picklist column.
Created the sample_String column.
Created the sample_MultiSelectOptionSet column.         

Sample code on GitHub

Work with attribute metadata

Remarks

Usage

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

Privileges and Access Rights

To perform this action, the caller must have the prvCreateAttribute privileges that are granted to the System Administrator and System Customizer security roles.

Notes for Callers

You can't use this message to create LookupAttributeMetadata columns. To create lookup columns, use these messages:

Supported Tables

You can only add attributes to customizable entities where the managed property CanCreateAttributes is true.

Constructors

CreateAttributeRequest()

Initializes a new instance of the CreateAttributeRequest class.

Properties

Attribute

Gets or sets the definition of the column type that you want to create. Required.

EntityName

Gets or sets the name of the table for which you want to create an column. Required.

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)
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 which you want to add this column. Optional.

Applies to

See also