在Dynamics 2011中检索选项集

时间:2011-03-29 09:34:04

标签: c# dynamics-crm dynamics-crm-2011

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

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?

2 个答案:

答案 0 :(得分:4)

你应该使用RetrieveAttributeRequest获得它。它将返回RetrieveAttributeResponse,其中包含属性AttributeMetadata

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

答案 1 :(得分:2)

这就是我解决这个问题的方法。 CRMBase是我的基类,与CRM实例连接。 Codelanguage: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();
}