3

我正在使用此代码来检索全局选项集

var request = new RetrieveOptionSetRequest {Name = "OptionsetNameGoesHere"};
var retrieveOptionSetResponse =(RetrieveOptionSetResponse) DynamicsHandler._serviceProxy.Execute(request);
var retrievedOptionSetMetadata =(OptionSetMetadata) retrieveOptionSetResponse.OptionSetMetadata;
var optionList = retrievedOptionSetMetadata.Options.ToArray();

foreach (var optionMetadata in optionList)
{
   Printout(optionMetadata.Label.LocalizedLabels[0].Label + "\n");
}

但是如何检索像 AccountCategory (AccountCategoryCode) 这样的选项集,以便将它们绑定到 Combobox?

4

2 回答 2

4

您应该使用RetrieveAttributeRequest来获取它。它将返回一个包含属性AttributeMetadata的RetrieveAttributeResponse

在您的情况下,它应该是您正在寻找的OptionSetMetadata类型。

于 2011-03-29T10:00:33.557 回答
2

这就是我解决这个问题的方法。CRMBase 是我连接到 CRM 实例的基类。代码语言:C#

public static Dictionary<int, string> GetAll(CRMBase conn, string entityName, string attributeName)
{
     OptionMetadataCollection result = RetrieveOptionSetMetaDataCollection(conn, entityName,           attributeName);
     return result.Where(r => r.Value.HasValue).ToDictionary(r => r.Value.Value, r => r.Label.UserLocalizedLabel.Label);
}

// Method to retrieve OptionSet Options Metadadata collection.
private static OptionMetadataCollection RetrieveOptionSetMetaDataCollection(CRMBase conn, string prmEntityName, string prmAttributeName)
{
     RetrieveEntityRequest retrieveEntityRequest = new RetrieveEntityRequest();

     retrieveEntityRequest.LogicalName = prmEntityName;

     retrieveEntityRequest.EntityFilters = Microsoft.Xrm.Sdk.Metadata.EntityFilters.Attributes;

     RetrieveEntityResponse retrieveEntityResponse = (RetrieveEntityResponse)conn._orgContext.Execute(retrieveEntityRequest);

     return (from AttributeMetadata in retrieveEntityResponse.EntityMetadata.Attributes where 
     (AttributeMetadata.AttributeType == AttributeTypeCode.Picklist & AttributeMetadata.LogicalName == prmAttributeName) 
     select ((PicklistAttributeMetadata)AttributeMetadata).OptionSet.Options).FirstOrDefault();
}
于 2014-11-05T11:55:13.130 回答