是,將數據全部存放在一個屬性(SDK article)的元數據。您必須檢索實體的實體元數據,然後在列表中找到該屬性。然後將該屬性轉換爲PicklistAttributeMetadata對象,它將包含一個選項列表。我會提到,通常從CRM中檢索元數據是一項昂貴的操作,因此請考慮緩存。
private static OptionSetMetadata RetrieveOptionSet(IOrganizationService orgService,
string entityName, string attributeName)
{
var entityResponse = (RetrieveEntityResponse)orgService.Execute(
new RetrieveEntityRequest
{ LogicalName = entityName, EntityFilters = EntityFilters.Attributes });
var entityMetadata = entityResponse.EntityMetadata;
for (int i = 0; i < entityMetadata.Attributes.Length; i++)
{
if (attributeName.Equals(entityMetadata.Attributes[i].LogicalName))
{
if (entityMetadata.Attributes[i].AttributeType.Value ==
AttributeTypeCode.Picklist)
{
var attributeMD = (PicklistAttributeMetadata)
entityMetadata.Attributes[i];
return attributeMD.OptionSet;
}
}
}
return null;
}
以下是如何使用上述調用將選項寫入控制檯的方法。
var optionSetMD = RetrieveOptionSet(orgService, "account", "accountcategorycode");
var options = optionSetMD.Options;
for (int i = 0; i < options.Count; i++)
{
Console.WriteLine("Local Label: {0}. Value: {1}",
options[i].Label.UserLocalizedLabel.Label,
options[i].Value.HasValue ? options[i].Value.Value.ToString() : "null");
}
我相信這個工程的全局選項設置屬性爲好,但如果你知道它是一個全球性的選項設置有它不同的消息可能會多一點效率(SDK article)。
的optionset是全球性的還是本地的?全局選項集的 – 2013-04-29 13:14:13