从CRM插件中的OptionSetValue获取字符串值

时间:2014-07-21 19:55:51

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

我想知道如何在我正在制作的CRM插件中获取OptionSet的字符串值。我认为我所要做的就是将int值传递给OptionSetValue,但这似乎并没有起作用。这是我的代码:

aBillingFrequencyCode = new OptionSetValue(myContract.BillingFrequencyCode.Value).ToString();

但输出只是

Microsoft.Xrm.Sdk.OptionSetValue

有什么想法吗?

2 个答案:

答案 0 :(得分:19)

您可以检索OptionSet标签​​而无需检索所有实体的元数据。我提供了两种方法。一个人将使用运行IOrganizationService的帐户的语言代码(LCID)。另一个允许您指定LCID。

注意,如果您要在代码中广泛使用这些代码,您可能需要考虑缓存该值以提高性能 - 这将取决于您的特定应用程序要求。

如果您计划同时在单个实体上检索多个选项集的这些值,则应使用上面的Guido代码并在一次调用中检索所有实体元数据,以减少您需要对CRM进行的调用次数。因此,在某些情况下,我们的每个代码段都更有效。

//This method will return the label for the LCID of the account the IOrganizationService is using
public static string GetOptionSetValueLabel(string entityName, string fieldName, int optionSetValue, IOrganizationService service)
{
    var attReq = new RetrieveAttributeRequest();
    attReq.EntityLogicalName = entityName;
    attReq.LogicalName = fieldName;
    attReq.RetrieveAsIfPublished = true;

    var attResponse = (RetrieveAttributeResponse)service.Execute(attReq);
    var attMetadata = (EnumAttributeMetadata)attResponse.AttributeMetadata;

    return attMetadata.OptionSet.Options.Where(x => x.Value == optionSetValue).FirstOrDefault().Label.UserLocalizedLabel.Label;
}

//This method will return the label for the specified LCID
public static string GetOptionSetValueLabel(string entityName, string fieldName, int optionSetValue, int lcid, IOrganizationService service)
{
    var attReq = new RetrieveAttributeRequest();
    attReq.EntityLogicalName = entityName;
    attReq.LogicalName = fieldName;
    attReq.RetrieveAsIfPublished = true;

    var attResponse = (RetrieveAttributeResponse)service.Execute(attReq);
    var attMetadata = (EnumAttributeMetadata)attResponse.AttributeMetadata;

    return attMetadata.OptionSet.Options.Where(x => x.Value == optionSetValue).FirstOrDefault().Label.LocalizedLabels.Where(l => l.LanguageCode == lcid).FirstOrDefault().Label;
}        

答案 1 :(得分:7)

要获取OptionSet文本值,您需要查询元数据(这是因为Dynamics CRM支持多种语言)

这是一个例子:

public static string GetoptionsetText(string entityName, string attributeName, int optionSetValue, IOrganizationService service)
{
    string AttributeName = attributeName;
    string EntityLogicalName = entityName;
    RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest
    {
        EntityFilters = EntityFilters.All,
        LogicalName = EntityLogicalName
    };
    RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails);
    Microsoft.Xrm.Sdk.Metadata.EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata;
    Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata;
    Microsoft.Xrm.Sdk.Metadata.OptionSetMetadata options = picklistMetadata.OptionSet;
    IList<OptionMetadata> OptionsList = (from o in options.Options
                                            where o.Value.Value == optionSetValue
                                            select o).ToList();
    string optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Label;
    return optionsetLabel;
}