CreateAttributeRequest Class

Applies To: Microsoft Dynamics CRM 2013, Microsoft Dynamics CRM Online

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

Namespace: Microsoft.Xrm.Sdk.Messages
Assembly: Microsoft.Xrm.Sdk (in Microsoft.Xrm.Sdk.dll)

Syntax

'Declaration
<DataContractAttribute(Namespace:="https://schemas.microsoft.com/xrm/2011/Contracts")> _
Public NotInheritable Class CreateAttributeRequest
    Inherits OrganizationRequest
[DataContractAttribute(Namespace="https://schemas.microsoft.com/xrm/2011/Contracts")] 
public sealed class CreateAttributeRequest : OrganizationRequest

Example

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. For the complete sample, see the link later in this topic.

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 attribute is created by using the Execute method.

This code sample assumes that the current customization prefix is ‘new’ because that is the default customization prefix for the organization solution publisher. You should use the customization prefix for the solution publisher that makes sense for the solution context in which you work.

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

// Create a boolean attribute
BooleanAttributeMetadata boolAttribute = new BooleanAttributeMetadata
{
    // Set base properties
    SchemaName = "new_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 attribute
DateTimeAttributeMetadata dtAttribute = new DateTimeAttributeMetadata
{
    // Set base properties
    SchemaName = "new_datetime",
    DisplayName = new Label("Sample DateTime", _languageCode),
    RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
    Description = new Label("DateTime Attribute", _languageCode),
    // Set extended properties
    Format = DateTimeFormat.DateOnly,
    ImeMode = ImeMode.Disabled
};

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

// Create a decimal attribute    
DecimalAttributeMetadata decimalAttribute = new DecimalAttributeMetadata
{
    // Set base properties
    SchemaName = "new_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 attribute    
IntegerAttributeMetadata integerAttribute = new IntegerAttributeMetadata
{
    // Set base properties
    SchemaName = "new_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 attribute 
MemoAttributeMetadata memoAttribute = new MemoAttributeMetadata
{
    // Set base properties
    SchemaName = "new_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 attribute    
MoneyAttributeMetadata moneyAttribute = new MoneyAttributeMetadata
{
    // Set base properties
    SchemaName = "new_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 picklist attribute    
PicklistAttributeMetadata pickListAttribute =
    new PicklistAttributeMetadata
{
    // Set base properties
    SchemaName = "new_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 attribute
StringAttributeMetadata stringAttribute = new StringAttributeMetadata
{
    // Set base properties
    SchemaName = "new_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);

// NOTE: LookupAttributeMetadata cannot be created outside the context of a relationship.
// Refer to the WorkWithRelationships.cs reference SDK sample for an example of this attribute type.

// NOTE: StateAttributeMetadata and StatusAttributeMetadata cannot be created via the SDK.

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

    // Execute the request.
    _serviceProxy.Execute(createAttributeRequest);

    Console.WriteLine("Created the attribute {0}.", anAttribute.SchemaName);
}
' Create storage for new attributes being created
addedAttributes = New List(Of AttributeMetadata)()

' Create a boolean attribute
Dim boolAttribute As BooleanAttributeMetadata = New BooleanAttributeMetadata With {
 .SchemaName = "new_boolean",
 .DisplayName = New Label("Sample Boolean", _languageCode),
 .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
 .Description = New Label("Boolean Attribute", _languageCode),
 .OptionSet = New BooleanOptionSetMetadata(
  New OptionMetadata(
   New Label("True", _languageCode), 1),
   New OptionMetadata(
   New Label("False", _languageCode), 0)
  )
}
' Set base properties
' Set extended properties

' Add to list
addedAttributes.Add(boolAttribute)

' Create a date time attribute
Dim dtAttribute As DateTimeAttributeMetadata = New DateTimeAttributeMetadata With {
 .SchemaName = "new_datetime",
 .DisplayName = New Label("Sample DateTime", _languageCode),
 .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
 .Description = New Label("DateTime Attribute", _languageCode),
 .Format = DateTimeFormat.DateOnly,
 .ImeMode = ImeMode.Disabled}
' Set base properties
' Set extended properties

' Add to list
addedAttributes.Add(dtAttribute)

' Create a decimal attribute    
Dim decimalAttribute As DecimalAttributeMetadata = New DecimalAttributeMetadata With {
 .SchemaName = "new_decimal",
 .DisplayName = New Label("Sample Decimal", _languageCode),
 .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
 .Description = New Label("Decimal Attribute", _languageCode),
 .MaxValue = 100,
 .MinValue = 0,
 .Precision = 1}
' Set base properties
' Set extended properties

' Add to list
addedAttributes.Add(decimalAttribute)

' Create a integer attribute    
Dim integerAttribute As IntegerAttributeMetadata = New IntegerAttributeMetadata With {
 .SchemaName = "new_integer",
 .DisplayName = New Label("Sample Integer", _languageCode),
 .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
 .Description = New Label("Integer Attribute", _languageCode),
 .Format = IntegerFormat.None,
 .MaxValue = 100,
 .MinValue = 0}
' Set base properties
' Set extended properties

' Add to list
addedAttributes.Add(integerAttribute)

' Create a memo attribute 
Dim memoAttribute As MemoAttributeMetadata = New MemoAttributeMetadata With {
 .SchemaName = "new_memo",
 .DisplayName = New Label("Sample Memo", _languageCode),
 .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
 .Description = New Label("Memo Attribute", _languageCode),
 .Format = StringFormat.TextArea,
 .ImeMode = ImeMode.Disabled,
 .MaxLength = 500}
' Set base properties
' Set extended properties

' Add to list
addedAttributes.Add(memoAttribute)

' Create a money attribute    
Dim moneyAttribute As MoneyAttributeMetadata = New MoneyAttributeMetadata With {
 .SchemaName = "new_money",
 .DisplayName = New Label("Money Picklist", _languageCode),
 .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
 .Description = New Label("Money Attribue", _languageCode),
 .MaxValue = 1000.0,
 .MinValue = 0.0,
 .Precision = 1,
 .PrecisionSource = 1,
 .ImeMode = ImeMode.Disabled}
' Set base properties
' Set extended properties

' Add to list
addedAttributes.Add(moneyAttribute)

' Create a picklist attribute    
Dim pickListAttribute As PicklistAttributeMetadata = New PicklistAttributeMetadata With {
 .SchemaName = "new_picklist",
 .DisplayName = New Label("Sample Picklist", _languageCode),
 .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
 .Description = New Label("Picklist Attribute", _languageCode)}
Dim pickListOptionSetMetadata As OptionSetMetadata = New OptionSetMetadata() With {
 .IsGlobal = False,
 .OptionSetType = OptionSetType.Picklist}
pickListOptionSetMetadata.Options.Add(New OptionMetadata(New Label("Created", _languageCode), Nothing))
pickListOptionSetMetadata.Options.Add(New OptionMetadata(New Label("Updated", _languageCode), Nothing))
pickListOptionSetMetadata.Options.Add(New OptionMetadata(New Label("Deleted", _languageCode), Nothing))
pickListAttribute.OptionSet = pickListOptionSetMetadata
' Set base properties
' Set extended properties
' Build local picklist options

' Add to list
addedAttributes.Add(pickListAttribute)

' Create a string attribute
Dim stringAttribute As StringAttributeMetadata = New StringAttributeMetadata With {
 .SchemaName = "new_string",
 .DisplayName = New Label("Sample String", _languageCode),
 .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
 .Description = New Label("String Attribute", _languageCode),
 .MaxLength = 100}
' Set base properties
' Set extended properties

' Add to list
addedAttributes.Add(stringAttribute)

' NOTE: LookupAttributeMetadata cannot be created outside the context of a relationship.
' Refer to the WorkWithRelationships.cs reference SDK sample for an example of this attribute type.

' NOTE: StateAttributeMetadata and StatusAttributeMetadata cannot be created via the SDK.

For Each anAttribute As AttributeMetadata In addedAttributes
 ' Create the request.
 Dim createAttributeRequest As CreateAttributeRequest = New CreateAttributeRequest With {
  .EntityName = Contact.EntityLogicalName,
  .Attribute = anAttribute}

 ' Execute the request.
 _serviceProxy.Execute(createAttributeRequest)

 Console.WriteLine("Created the attribute {0}.", anAttribute.SchemaName)
Next anAttribute

Remarks

Message Availability

For this message to work, the caller must be connected to the server.

Usage

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

Privileges and Access Rights

To perform this action, the caller must have the required privileges, as listed in CreateAttribute Privileges.

Notes for Callers

This message cannot be used to create a LookupAttributeMetadata attribute. Use the CreateOneToManyRequest message to create an entity relationship that includes the lookup attribute.

Supported Entities

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

Inheritance Hierarchy

System.Object
   Microsoft.Xrm.Sdk.OrganizationRequest
    Microsoft.Xrm.Sdk.Messages.CreateAttributeRequest

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Platforms

Development Platforms

Windows Server 2008, Windows Server 2012, Windows 7 (All Versions), Windows 8 (All Versions)

Target Platforms

Windows Server 2008, ,Windows Server 2012, ,Windows 7 (All Versions),

Change History

See Also

Reference

CreateAttributeRequest Members
Microsoft.Xrm.Sdk.Messages Namespace
CreateAttributeResponse

Other Resources

Customize Entity Attribute Metadata
Entity Attribute Metadata Messages
Sample: Create and Update Entity Metadata
CreateAttribute Privileges
Introduction to Solutions

Send comments about this topic to Microsoft.
© 2013 Microsoft Corporation. All rights reserved.