CreateAttribute Message (MetadataService)

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

Works for all deployment types Works online only

Creates a custom attribute for an entity.

The relevant classes are specified in the following table.

Type Class
Request CreateAttributeRequest
Response CreateAttributeResponse

Remarks

To perform this action, the caller must be a user in the organization for which metadata is requested and must have Create Attribute and Read Attribute privileges.

The attribute created will have a property value of true for the AttributeMetadata.IsCustomField property.

You must publish the changes to the metadata before this change will be visible in the application. For more information see Publishing the Metadata.

The following attribute metadata subclasses are valid for this message.

Valid Attribute subclasses
BooleanAttributeMetadata
DateTimeAttributeMetadata
DecimalAttributeMetadata
FloatAttributeMetadata
IntegerAttributeMetadata
MemoAttributeMetadata
MoneyAttributeMetadata
PicklistAttributeMetadata
StringAttributeMetadata

Example

The following example shows how to use the CreateAttribute message to create a picklist attribute. For more information on the helper methods in the Microsoft.Crm.Sdk.Utility.CrmServiceUtility namespace, see Utility Sample Code.

[C#]
// Create an authentication token.
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.OrganizationName = "AdventureWorksCycle";

// You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0; 

// Create the metadata Web service.
MetadataService metadataService = new MetadataService();
metadataService.Url = "http://<servername>:<port>/MSCRMServices/2007/MetadataService.asmx";
metadataService.CrmAuthenticationTokenValue = token;
metadataService.Credentials = System.Net.CredentialCache.DefaultCredentials;
metadataService.PreAuthenticate = true;

// Create a picklist attribute.
PicklistAttributeMetadata pickListAttribute = new PicklistAttributeMetadata();

// Set base properties.
pickListAttribute.SchemaName = "new_picklist";
pickListAttribute.DisplayName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Sample Picklist", 1033);
pickListAttribute.RequiredLevel = new CrmAttributeRequiredLevel();
pickListAttribute.RequiredLevel.Value = AttributeRequiredLevel.None;
pickListAttribute.Description = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Picklist Attribute", 1033);
pickListAttribute.AttributeType = new MetadataServiceSdk.CrmAttributeType();
pickListAttribute.AttributeType.Value = AttributeType.Picklist;

// Build picklist options.
string[] englishLabels = new string[] { "created", "updated", "deleted" };
MetadataServiceSdk.Option[] picklistOptions = new MetadataServiceSdk.Option[englishLabels.Length];
MetadataServiceSdk.Option picklistOption;

for (int i = 0; i < englishLabels.Length; i++)
{
   picklistOption = new MetadataServiceSdk.Option();
   picklistOption.Value = new MetadataServiceSdk.CrmNumber();
   picklistOption.Value.Value = i + 1;
   picklistOption.Label = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel(englishLabels[i], 1033);
   picklistOptions[i] = picklistOption;
}

// Set extended properties.
pickListAttribute.Options = picklistOptions;

// Create the request/response objects.
CreateAttributeRequest createAttribute = new CreateAttributeRequest();
CreateAttributeResponse attributeResponse = null;

// Set the request properties 
createAttribute.EntityName = EntityName.contact.ToString();
createAttribute.Attribute = pickListAttribute;

// Execute the request.
attributeResponse = (CreateAttributeResponse)metadataService.Execute(createAttribute);

// Publish the new customizations.
// Note that the custom attributes cannot be used until they have been published.
PublishAllXmlRequest publishAllRequest = new PublishAllXmlRequest();
PublishAllXmlResponse publishAllResponse = (PublishAllXmlResponse)metadataService.Execute(publishAllRequest);

[Visual Basic .NET]
' Set up the CRM service.
Dim token As New CrmAuthenticationToken()
' You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0
token.OrganizationName = "AdventureWorksCycle";

// Create the metadata Web service.
Dim metadataService As New MetadataService ()
metadataService.Url = "http://<servername>:<port>/mscrmservices/2007/MetadataService.asmx";
metadataService.CrmAuthenticationTokenValue = token;
metadataService.Credentials = System.Net.CredentialCache.DefaultCredentials
metadataService.PreAuthenticate = true;

// Set timeout to 90 seconds.
metadataService.Timeout = 90000;

// Create storage for all attributes being created.
ArrayList allAttributes = new ArrayList();

// Create a Boolean attribute.
BooleanAttributeMetadata boolAttribute = new BooleanAttributeMetadata();

// Set base properties.
boolAttribute.SchemaName = "new_boolean";
boolAttribute.DisplayName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Sample Boolean", 1033);
boolAttribute.RequiredLevel = new CrmAttributeRequiredLevel();
boolAttribute.RequiredLevel.Value = AttributeRequiredLevel.None;
boolAttribute.Description = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Boolean Attribute", 1033);
boolAttribute.AttributeType = new MetadataServiceSdk.CrmAttributeType();
boolAttribute.AttributeType.Value = AttributeType.Boolean;

// Set extended properties.
boolAttribute.FalseOption = new MetadataServiceSdk.Option();
boolAttribute.FalseOption.Label = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("False", 1033);
boolAttribute.FalseOption.Value = new MetadataServiceSdk.CrmNumber();
boolAttribute.FalseOption.Value.Value = 0;
boolAttribute.TrueOption = new MetadataServiceSdk.Option();
boolAttribute.TrueOption.Label = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("True", 1033);
boolAttribute.TrueOption.Value = new MetadataServiceSdk.CrmNumber();
boolAttribute.TrueOption.Value.Value = 1;

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

// Create a date time attribute.
DateTimeAttributeMetadata dtAttribute = new DateTimeAttributeMetadata();

// Set base properties.
dtAttribute.SchemaName = "new_datetime";
dtAttribute.DisplayName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Sample DateTime", 1033);
dtAttribute.RequiredLevel = new CrmAttributeRequiredLevel();
dtAttribute.RequiredLevel.Value = AttributeRequiredLevel.None;
dtAttribute.Description = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("DateTime Attribute", 1033);
dtAttribute.AttributeType = new MetadataServiceSdk.CrmAttributeType();
dtAttribute.AttributeType.Value = AttributeType.DateTime;

// Set extended properties.
dtAttribute.Format = new MetadataServiceSdk.CrmDateTimeFormat();
dtAttribute.Format.Value = DateTimeFormat.DateOnly;
dtAttribute.ImeMode = new MetadataServiceSdk.CrmImeMode();
dtAttribute.ImeMode.Value = ImeMode.Disabled;

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

// Create a decimal attribute.
DecimalAttributeMetadata decimalAttribute = new DecimalAttributeMetadata();

// Set base properties.
decimalAttribute.SchemaName = "new_decimal";
decimalAttribute.DisplayName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Sample Decimal", 1033);
decimalAttribute.RequiredLevel = new CrmAttributeRequiredLevel();
decimalAttribute.RequiredLevel.Value = AttributeRequiredLevel.None;
decimalAttribute.Description = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Decimal Attribute", 1033);
decimalAttribute.AttributeType = new MetadataServiceSdk.CrmAttributeType();
decimalAttribute.AttributeType.Value = AttributeType.Decimal;

// Set extended properties.
decimalAttribute.MaxValue = new MetadataServiceSdk.CrmDouble();
decimalAttribute.MaxValue.Value = 100;
decimalAttribute.MinValue = new MetadataServiceSdk.CrmDouble();
decimalAttribute.MinValue.Value = 0;
decimalAttribute.Precision = new MetadataServiceSdk.CrmNumber();
decimalAttribute.Precision.Value = 1;

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

// Create a float attribute.
FloatAttributeMetadata floatAttribute = new FloatAttributeMetadata();

// Set base properties.
floatAttribute.SchemaName = "new_float";
floatAttribute.DisplayName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Sample Float", 1033);
floatAttribute.RequiredLevel = new CrmAttributeRequiredLevel();
floatAttribute.RequiredLevel.Value = AttributeRequiredLevel.None;
floatAttribute.Description = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Float Attribute", 1033);
floatAttribute.AttributeType = new MetadataServiceSdk.CrmAttributeType();
floatAttribute.AttributeType.Value = AttributeType.Float;

// Set extended properties.
floatAttribute.MaxValue = new MetadataServiceSdk.CrmFloat();
floatAttribute.MaxValue.Value = 100.00f;
floatAttribute.MinValue = new MetadataServiceSdk.CrmFloat();
floatAttribute.MinValue.Value = 0.00f;
floatAttribute.Precision = new MetadataServiceSdk.CrmNumber();
floatAttribute.Precision.Value = 2;
floatAttribute.ImeMode = new MetadataServiceSdk.CrmImeMode();
floatAttribute.ImeMode.Value = ImeMode.Disabled;

// Add to list.
allAttributes.Add(floatAttribute);

// Create a integer attribute.
IntegerAttributeMetadata integerAttribute = new IntegerAttributeMetadata();

// Set base properties.
integerAttribute.SchemaName = "new_integer";
integerAttribute.DisplayName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Sample Integer", 1033);
integerAttribute.RequiredLevel = new CrmAttributeRequiredLevel();
integerAttribute.RequiredLevel.Value = AttributeRequiredLevel.None;
integerAttribute.Description = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Integer Attribute", 1033);
integerAttribute.AttributeType = new MetadataServiceSdk.CrmAttributeType();
integerAttribute.AttributeType.Value = AttributeType.Integer;

// Set extended properties.
integerAttribute.Format = new MetadataServiceSdk.CrmIntegerFormat();
integerAttribute.Format.Value = IntegerFormat.None;
integerAttribute.MaxValue = new MetadataServiceSdk.CrmNumber();
integerAttribute.MaxValue.Value = 100;
integerAttribute.MinValue = new MetadataServiceSdk.CrmNumber();
integerAttribute.MinValue.Value = 0;

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

// Create a memo attribute.
MemoAttributeMetadata memoAttribute = new MemoAttributeMetadata();

// Set base properties.
memoAttribute.SchemaName = "new_memo";
memoAttribute.DisplayName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Sample Memo", 1033);
memoAttribute.RequiredLevel = new CrmAttributeRequiredLevel();
memoAttribute.RequiredLevel.Value = AttributeRequiredLevel.None;
memoAttribute.Description = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Memo Attribute", 1033);
memoAttribute.AttributeType = new MetadataServiceSdk.CrmAttributeType();
memoAttribute.AttributeType.Value = AttributeType.Memo;

// Set extended properties.
memoAttribute.Format = new MetadataServiceSdk.CrmStringFormat();
// Note: The Format for a memo must be TextArea.
memoAttribute.Format.Value = StringFormat.TextArea;
memoAttribute.ImeMode = new MetadataServiceSdk.CrmImeMode();
memoAttribute.ImeMode.Value = ImeMode.Disabled;
memoAttribute.MaxLength = new MetadataServiceSdk.CrmNumber();
memoAttribute.MaxLength.Value = 500;

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

// Create a money attribute.
MoneyAttributeMetadata moneyAttribute = new MoneyAttributeMetadata();

// Set base properties.
moneyAttribute.SchemaName = "new_money";
moneyAttribute.DisplayName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Money Picklist", 1033);
moneyAttribute.RequiredLevel = new CrmAttributeRequiredLevel();
moneyAttribute.RequiredLevel.Value = AttributeRequiredLevel.None;
moneyAttribute.Description = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Money Attribute", 1033);
moneyAttribute.AttributeType = new MetadataServiceSdk.CrmAttributeType();
moneyAttribute.AttributeType.Value = AttributeType.Money;

// Set extended properties.
moneyAttribute.MaxValue = new MetadataServiceSdk.CrmDouble();
moneyAttribute.MaxValue.Value = 1000.00;
moneyAttribute.MinValue = new MetadataServiceSdk.CrmDouble();
moneyAttribute.MinValue.Value = 0.00;
moneyAttribute.Precision = new MetadataServiceSdk.CrmNumber();
moneyAttribute.Precision.Value = 1;
moneyAttribute.PrecisionSource = new MetadataServiceSdk.CrmNumber();
moneyAttribute.PrecisionSource.Value = 1;
moneyAttribute.ImeMode = new MetadataServiceSdk.CrmImeMode();
moneyAttribute.ImeMode.Value = ImeMode.Disabled;
         
// Add to list.
allAttributes.Add(moneyAttribute);

// Create a picklist attribute.
PicklistAttributeMetadata pickListAttribute = new PicklistAttributeMetadata();

// Set base properties.
pickListAttribute.SchemaName = "new_picklist";
pickListAttribute.DisplayName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Sample Picklist", 1033);
pickListAttribute.RequiredLevel = new CrmAttributeRequiredLevel();
pickListAttribute.RequiredLevel.Value = AttributeRequiredLevel.None;
pickListAttribute.Description = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Picklist Attribute", 1033);
pickListAttribute.AttributeType = new MetadataServiceSdk.CrmAttributeType();
pickListAttribute.AttributeType.Value = AttributeType.Picklist;

// Build picklist options.
string[] englishLabels = new string[] { "created", "updated", "deleted" };
MetadataServiceSdk.Option[] picklistOptions = new MetadataServiceSdk.Option[englishLabels.Length];
MetadataServiceSdk.Option picklistOption;

for (int i = 0; i < englishLabels.Length; i++)
{
   picklistOption = new MetadataServiceSdk.Option();
   picklistOption.Value = new MetadataServiceSdk.CrmNumber();
   picklistOption.Value.Value = i + 1;
   picklistOption.Label = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel(englishLabels[i], 1033);
   picklistOptions[i] = picklistOption;
}

// Set extended properties.
pickListAttribute.Options = picklistOptions;

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

// Create a string attribute.
StringAttributeMetadata stringAttribute = new StringAttributeMetadata();
stringAttribute.SchemaName = "new_string";
stringAttribute.DisplayName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Sample String", 1033);
stringAttribute.RequiredLevel = new CrmAttributeRequiredLevel();
stringAttribute.RequiredLevel.Value = AttributeRequiredLevel.None;
stringAttribute.Description = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("String Attribute", 1033);
stringAttribute.AttributeType = new MetadataServiceSdk.CrmAttributeType();
stringAttribute.AttributeType.Value = AttributeType.String;

// Set extended properties.
stringAttribute.MaxLength = new MetadataServiceSdk.CrmNumber();
stringAttribute.MaxLength.Value = 100;

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

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

// Note: StateAttributeMetadata and StatusAttributeMetadata cannot be created via the SDK.
         
// Create the request/response objects.
CreateAttributeRequest createAttribute = new CreateAttributeRequest();
CreateAttributeResponse attributeResponse = null;

// Create the publish request.
PublishAllXmlRequest publishAllRequest = new PublishAllXmlRequest();

// Set the request properties. 
createAttribute.EntityName = EntityName.contact.ToString();
foreach(AttributeMetadata anAttribute in allAttributes)
{
   createAttribute.Attribute = anAttribute;
   
   // Execute the request.
   attributeResponse = (CreateAttributeResponse)metadataService.Execute(createAttribute);
   
   // Publish the new customizations.
   // Note:  The custom attributes cannot be used until they have been published.
   metadataService.Execute(publishAllRequest);
}

See Also

Concepts

Reference

© 2010 Microsoft Corporation. All rights reserved.