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

问题描述 投票:0回答:3

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

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

但输出只是

Microsoft.Xrm.Sdk.OptionSetValue

有什么想法吗?

c# dynamics-crm crm dynamics-crm-2013
3个回答
21
投票

您可以检索选项集标签,而无需检索实体的所有元数据。我提供了两种方法。将使用 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;
}        

7
投票

为了获取选项集文本值,您需要查询元数据(这是因为 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;
}

0
投票

考虑到有

entity.FormattedValues["field_name"]
,这很容易,您只需要识别字段类型并从中获取字符串即可。

var account = service.Retrieve("account",Guid.Parse(""),new ColumnSet(true));

var statcode = account.Attributes
    .Where(a => a.Value.GetType() == typeof(OptionSetValue) && a.Key == "statecode")
    .Select(f=> account.FormattedValues[f.Key])
    .First();

上面的代码将识别过滤器并从

statecode
实体中获取
account
文本。

© www.soinside.com 2019 - 2024. All rights reserved.