CreateAttributeRequest 类
应用到: CRM 2015 on-prem, CRM Online
Contains the data that is needed to create a new attribute, and optionally, to add it to a specified unmanaged solution.
命名空间: Microsoft.Xrm.Sdk.Messages
程序集: Microsoft.Xrm.Sdk (在 Microsoft.Xrm.Sdk.dll 中)
语法
声明
<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
示例
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);
}
备注
Message Availability
若要使此消息正常工作,调用方必须连接到服务器。
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.
继承层次结构
System.Object
Microsoft.Xrm.Sdk.OrganizationRequest
Microsoft.Xrm.Sdk.Messages.CreateAttributeRequest
线程安全性
此类型的所有公共静态(Visual Basic 中的 Shared)成员都是线程安全成员。不保证任何实例成员是线程安全成员。
平台
开发平台
Windows Vista、Windows Server 2003 和
目标平台
Windows Vista,Windows XP
Change History
另请参阅
参考
CreateAttributeRequest 成员
Microsoft.Xrm.Sdk.Messages 命名空间
CreateAttributeResponse
其他资源
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.
© 2014 Microsoft Corporation. All rights reserved.