【问题标题】:Get the string value from OptionSetValue in CRM Plugin从 CRM Plugin 中的 OptionSetValue 获取字符串值
【发布时间】:2014-07-21 19:55:51
【问题描述】:

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

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

但输出只是

Microsoft.Xrm.Sdk.OptionSetValue

有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

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

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

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

    //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;
    }        
    

    【讨论】:

    • 我见过使用entity.FormattedValues["optionSet"] 获取所选选项标签的示例。由于我看到更多的人使用请求服务来检索这些数据,我认为这可能是它应该完成的方式,但我只是想知道,有什么区别?
    • 如果您是实体,您可以使用entity.FormattedValues["optionSet"] 获取该特定记录的选项集值的选项集标签。您的问题更笼统地理解为如何获取特定选项集值的标签 - 不特定于实体记录 - 这就是我发布代码的原因。
    • 我不是 OP,我对最佳实践很好奇。
    • 如果您想使用 FormattedAttributes 选项,您必须了解实体的来源。如果您使用组织服务检索实体,您将在那里找到您的值,如果它是从插件执行上下文中检索的目标,您的 FormattedValue 将不存在。如果它是 PreImage/PostImage,它将类似于从组织服务中检索到的记录。
    • @jhoefnagels 最近我在一个解决案例的插件中使用了string label = target.FormattedValues["resolutiontypecode"] - 创建并且它有效! (没有图像,没有检索实体)自从您发表评论以来,它似乎有所改进。
    【解决方案2】:

    为了获取 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      • 2013-11-20
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多