使用属性元数据
发布日期: 2017年1月
适用于: Dynamics 365 (online),Dynamics 365 (on-premises),Dynamics CRM 2016,Dynamics CRM Online
本主题使用示例:使用属性元数据中的代码段演示如何在使用属性时执行常见任务。
本主题内容
创建属性
检索属性
更新属性
创建查找属性
创建客户查找属性
更新状态值
创建使用全局选项集的选择列表
插入新状态值
在本地选项集中插入新选项
更改本地选项集中选项的顺序
删除属性
创建属性
可以通过定义一种 AttributeMetadata 类型然后将其传递给 CreateAttributeRequest 消息来创建属性。
以下示例可为许多不同属性类型定义 AttributeMetadata,并将其添加到 List<AttributeMetadata> 中。 在代码末尾,将属性定义传递给 CreateAttributeRequest 类的一个实例,并使用 Execute 方法创建属性。
以下示例假设当前自定义前缀为“new”,因为这是组织解决方案发布商的默认自定义前缀。 应针对解决方案发布商使用对您的解决方案上下文有意义的自定义前缀。
// 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);
}
检索属性
此示例演示如何使用 RetrieveAttributeRequest 检索属性的 AttributeMetadata。 此示例从在创建属性中创建的 Contact 实体中为名为“new_string”的自定义 StringAttributeMetadata 属性检索元数据。
备注
由于 RetrieveAsIfPublished 为 true,此请求将返回此属性的当前未发布定义。 如果要创建 Attribute 编辑器并且要检索属性的未发布定义,则可使用此属性。 否则,不应指定 RetrieveAsIfPublished。详细信息:检索未发布的元数据
// Create the request
RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest
{
EntityLogicalName = Contact.EntityLogicalName,
LogicalName = "new_string",
RetrieveAsIfPublished = true
};
// Execute the request
RetrieveAttributeResponse attributeResponse =
(RetrieveAttributeResponse)_serviceProxy.Execute(attributeRequest);
Console.WriteLine("Retrieved the attribute {0}.",
attributeResponse.AttributeMetadata.SchemaName);
更新属性
此示例演示如何更新属性。 此示例使用 UpdateAttributeRequest 为 Contact 实体更改以前检索的自定义属性的 AttributeMetadata.DisplayName 属性。
// Modify the retrieved attribute
AttributeMetadata retrievedAttributeMetadata =
attributeResponse.AttributeMetadata;
retrievedAttributeMetadata.DisplayName =
new Label("Update String Attribute", _languageCode);
// Update an attribute retrieved via RetrieveAttributeRequest
UpdateAttributeRequest updateRequest = new UpdateAttributeRequest
{
Attribute = retrievedAttributeMetadata,
EntityName = Contact.EntityLogicalName,
MergeLabels = false
};
// Execute the request
_serviceProxy.Execute(updateRequest);
Console.WriteLine("Updated the attribute {0}.",
retrievedAttributeMetadata.SchemaName);
创建查找属性
使用 CreateOneToManyRequest 可创建查找属性。
此示例代码显示如何创建查找属性。 有关完整示例,请参阅 示例:创建和更新实体元数据
CreateOneToManyRequest req = new CreateOneToManyRequest()
{
Lookup = new LookupAttributeMetadata()
{
Description = new Label("The referral (lead) from the bank account owner", 1033),
DisplayName = new Label("Referral", 1033),
LogicalName = "new_parent_leadid",
SchemaName = "New_Parent_leadId",
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.Recommended)
},
OneToManyRelationship = new OneToManyRelationshipMetadata()
{
AssociatedMenuConfiguration = new AssociatedMenuConfiguration()
{
Behavior = AssociatedMenuBehavior.UseCollectionName,
Group = AssociatedMenuGroup.Details,
Label = new Label("Bank Accounts", 1033),
Order = 10000
},
CascadeConfiguration = new CascadeConfiguration()
{
Assign = CascadeType.Cascade,
Delete = CascadeType.Cascade,
Merge = CascadeType.Cascade,
Reparent = CascadeType.Cascade,
Share = CascadeType.Cascade,
Unshare = CascadeType.Cascade
},
ReferencedEntity = "lead",
ReferencedAttribute = "leadid",
ReferencingEntity = _customEntityName,
SchemaName = "new_lead_new_bankaccount"
}
};
_serviceProxy.Execute(req);
创建客户查找属性
不同于查找属性,客户查找属性使用 CreateCustomerRelationshipsRequest 消息创建,添加两个关系到查找属性中:一个添加到 Account 实体,另外一个添加到 Contact 实体。 您不能添加关系到其他任何实体,除非是针对客户查找属性的 Account 和 Contact。
备注
CRM Online 2016 更新 1 和 CRM 2016 Service Pack 1(本地)中引入了此功能。
此示例代码显示如何创建客户查找属性。 有关完整示例,请参阅 示例:创建和更新实体元数据
CreateCustomerRelationshipsRequest createCustomerReq = new CreateCustomerRelationshipsRequest
{
Lookup = new LookupAttributeMetadata
{
Description = new Label("The owner of the bank account", 1033),
DisplayName = new Label("Account owner", 1033),
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired),
SchemaName = "new_customerid"
},
OneToManyRelationships = new OneToManyRelationshipMetadata[]
{
new OneToManyRelationshipMetadata()
{
ReferencedEntity = "account",
ReferencingEntity = _customEntityName,
SchemaName = "new_bankaccount_customer_account",
},
new OneToManyRelationshipMetadata()
{
ReferencedEntity = "contact",
ReferencingEntity = _customEntityName,
SchemaName = "new_bankaccount_customer_contact",
}
},
};
_serviceProxy.Execute(createCustomerReq);
创建使用全局选项集的选择列表
此属性演示如何创建与全局选项集关联的 PicklistAttributeMetadata 属性。
以下示例使用 CreateAttributeRequest 为 PicklistAttributeMetadata 属性设置选项,以便使用其名称由字符串变量 _globalOptionSetName 表示的全局选项集。详细信息:配置全局选项集
// Create a Picklist linked to the option set.
// Specify which entity will own the picklist, and create it.
CreateAttributeRequest createRequest = new CreateAttributeRequest
{
EntityName = Contact.EntityLogicalName,
Attribute = new PicklistAttributeMetadata
{
SchemaName = "sample_examplepicklist",
LogicalName = "sample_examplepicklist",
DisplayName = new Label("Example Picklist", _languageCode),
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
// In order to relate the picklist to the global option set, be sure
// to specify the two attributes below appropriately.
// Failing to do so will lead to errors.
OptionSet = new OptionSetMetadata
{
IsGlobal = true,
Name = _globalOptionSetName
}
}
};
_serviceProxy.Execute(createRequest);
插入新状态值
此示例演示如何插入 StatusAttributeMetadata 属性的新“状态原因”选项。
以下示例使用 InsertStatusValueRequest 指定当 Contact.StateCode 为 0(活动)时有效的 Contact 实体的 Contact.StatusCode 属性的新选项。IOrganizationService.Execute 方法处理该请求。
以下示例允许活动联系人有两个有效“状态原因”选项:“活动”和Dormant.
// Use InsertStatusValueRequest message to insert a new status
// in an existing status attribute.
// Create the request.
InsertStatusValueRequest insertStatusValueRequest =
new InsertStatusValueRequest
{
AttributeLogicalName = "statuscode",
EntityLogicalName = Contact.EntityLogicalName,
Label = new Label("Dormant", _languageCode),
StateCode = 0
};
// Execute the request and store newly inserted value
// for cleanup, used later part of this sample.
_insertedStatusValue = ((InsertStatusValueResponse)_serviceProxy.Execute(
insertStatusValueRequest)).NewOptionValue;
Console.WriteLine("Created {0} with the value of {1}.",
insertStatusValueRequest.Label.LocalizedLabels[0].Label,
_insertedStatusValue);
更新状态值
此示例演示如何更改 StateAttributeMetadata 属性中选项的标签。
以下示例使用 UpdateStateValueRequest 将 Contact.StateCode 选项标签从“活动”更改为“打开”。
// Modify the state value label from Active to Open.
// Create the request.
UpdateStateValueRequest updateStateValue = new UpdateStateValueRequest
{
AttributeLogicalName = "statecode",
EntityLogicalName = Contact.EntityLogicalName,
Value = 1,
Label = new Label("Open", _languageCode)
};
// Execute the request.
_serviceProxy.Execute(updateStateValue);
Console.WriteLine(
"Updated {0} state attribute of {1} entity from 'Active' to '{2}'.",
updateStateValue.AttributeLogicalName,
updateStateValue.EntityLogicalName,
updateStateValue.Label.LocalizedLabels[0].Label
);
不能添加或删除 StateCode 选项,但可以更改选项的标签。
在本地选项集中插入新选项
此示例演示如何将新选项添加到本地选项集中。 以下示例使用 InsertOptionValueRequest 将新选项添加到 Contact 实体的自定义 PicklistAttributeMetadata 属性。
// Create a request.
InsertOptionValueRequest insertOptionValueRequest =
new InsertOptionValueRequest
{
AttributeLogicalName = "new_picklist",
EntityLogicalName = Contact.EntityLogicalName,
Label = new Label("New Picklist Label", _languageCode)
};
// Execute the request.
int insertOptionValue = ((InsertOptionValueResponse)_serviceProxy.Execute(
insertOptionValueRequest)).NewOptionValue;
Console.WriteLine("Created {0} with the value of {1}.",
insertOptionValueRequest.Label.LocalizedLabels[0].Label,
insertOptionValue);
更改本地选项集中选项的顺序
此示例演示如何更改本地选项集中选项的顺序。 以下示例检索自定义 PicklistAttributeMetadata 属性并使用 OrderByLINQ 函数按照标签文本的升序对项目进行排序,以更改原始选项的顺序。 然后,该示例使用 OrderOptionRequest 为属性设置选项的新顺序。
使用 OrderByDecending linq 函数可按降序对项目进行排序。
// Use the RetrieveAttributeRequest message to retrieve
// a attribute by it's logical name.
RetrieveAttributeRequest retrieveAttributeRequest =
new RetrieveAttributeRequest
{
EntityLogicalName = Contact.EntityLogicalName,
LogicalName = "new_picklist",
RetrieveAsIfPublished = true
};
// Execute the request.
RetrieveAttributeResponse retrieveAttributeResponse =
(RetrieveAttributeResponse)_serviceProxy.Execute(
retrieveAttributeRequest);
// Access the retrieved attribute.
PicklistAttributeMetadata retrievedPicklistAttributeMetadata =
(PicklistAttributeMetadata)
retrieveAttributeResponse.AttributeMetadata;
// Get the current options list for the retrieved attribute.
OptionMetadata[] optionList =
retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
// Change the order of the original option's list.
// Use the OrderBy (OrderByDescending) linq function to sort options in
// ascending (descending) order according to label text.
// For ascending order use this:
var updateOptionList =
optionList.OrderBy(x => x.Label.LocalizedLabels[0].Label).ToList();
// For descending order use this:
// var updateOptionList =
// optionList.OrderByDescending(
// x => x.Label.LocalizedLabels[0].Label).ToList();
// Create the request.
OrderOptionRequest orderOptionRequest = new OrderOptionRequest
{
// Set the properties for the request.
AttributeLogicalName = "new_picklist",
EntityLogicalName = Contact.EntityLogicalName,
// Set the changed order using Select linq function
// to get only values in an array from the changed option list.
Values = updateOptionList.Select(x => x.Value.Value).ToArray()
};
// Execute the request
_serviceProxy.Execute(orderOptionRequest);
Console.WriteLine("Option Set option order changed");
删除属性
此示例演示如何删除存储在 List<AttributeMetadata> 中的属性,这些属性是在创建属性中为 Contact 实体创建的。 对于每个 AttributeMetadata,DeleteAttributeRequest 都会准备使用 IOrganizationService.Execute 处理的请求。
// Delete all attributes created for this sample.
foreach (AttributeMetadata anAttribute in addedAttributes)
{
// Create the request object
DeleteAttributeRequest deleteAttribute = new DeleteAttributeRequest
{
// Set the request properties
EntityLogicalName = Contact.EntityLogicalName,
LogicalName = anAttribute.SchemaName
};
// Execute the request
_serviceProxy.Execute(deleteAttribute);
}
另请参阅
自定义实体属性元数据
实体属性元数据消息
示例:使用属性元数据
自定义实体和属性映射
Microsoft Dynamics 365
© 2017 Microsoft。 保留所有权利。 版权