• MS CRM 2011 如何获得Option Set的Label与Value


    原创地址:http://www.cnblogs.com/jfzhu/archive/2012/12/11/2813712.html

    转载请注明出处

    CRM中有一种Field的类型是Option Set。每一个option都是由一对 label + value组成的。比如我下面图中的Option Set,它的第一个option的Label是Logistic – Incorrect item,而它的value为267060000。

    image

    那么CRM SDK为我们提供了哪写方法可以获得一个Option Set 的Label和Value值呢?我在案例(incident)中创建了一个custom field aw_complaintcause,它是Option Set类型,而它所使用的Option Set即为上面图中创造的Case Option Set。

    image

    下面我分别演示一下在CRM的前端与后端如何获得Option Set 的Label 与 Value。

    (一)前端

    如果是使用JScript获得某个Option Set field的Label 或者 Value,比如在Form的OnLoad事件处理器中,可以用

    获得 Label:

    Xrm.Page.getAttribute(fieldname).getText();

    获得 Value:

    Xrm.Page.getAttribute(fieldname).getValue();

    如果Option Set为Unassigned Value,上面方法获得Label为空字符””,Value值为null。

    我们也可以遍历一个Option Set 的所有options:

    var objControl = Xrm.Page.getControl(fieldname); 
    var objOptions = objControl.getAttribute().getOptions();
    
    for (var i = 0; i < objOptions.length; i++) { 
        alert(objOptions[i].text); 
        alert(objOptions[i].value); 
    } 

    (二) 后端

    运行在服务器上的代码(C#或者VB)该如何获得某个Option Set的Label或Value呢?

    以插件(plugin)中的代码为例,获得Value很简单,可以使用

    int optionValue = ((OptionSetValue)entity[fieldname]).Value;

    代码的片断如下所示:

    if (context.MessageName == "Update") 
    { 
        Entity entity = (Entity)context.InputParameters["Target"]; 
        if (entity.Contains(fieldname) && entity[fieldname] != null) 
        { 
            int optionValue = ((OptionSetValue)entity[fieldname]).Value; 
        }                                        
    }

    如果Option Set为Unassigned Value,则entity[fieldname] 为 null。

    要获得Label就不象用JScript那样简单了,我们首先要知道当前option的value值,然后发送一个RetrieveAttributeRequest,在RetrieveAttributeResponse中获得AttributeMetadata,然后遍历Option Set的每一个option,当某个option的Value值与我们的value相等时,返回该option的Label。可以使用下面的代码来获得Label

    private string GetOptionLabel(IOrganizationService service, ITracingService tracingService, string entityname, string attributename, int value) 
    {
    
        RetrieveAttributeRequest retrieveAttributeRequest = 
            new RetrieveAttributeRequest 
            { 
                EntityLogicalName = entityname, 
                LogicalName = attributename 
            };
    
        // Execute the request. 
        RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest); 
        tracingService.Trace("Debug: Entity {0} Attribute {1} is retrieved", entityname, attributename);
    
        OptionMetadataCollection options = ((PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata).OptionSet.Options; 
    
        OptionMetadata option = options.Where(x => x.Value == value).FirstOrDefault(); 
        tracingService.Trace("Debug: Option of value {0} is retrieved", value);
    
        if (option != null) 
        { 
            tracingService.Trace("Debug: option != null"); 
            return option.Label.UserLocalizedLabel.Label; 
        } 
        else 
        { 
            return ""; 
        } 
    } 

    上面的代码返回的是option.Label.UserLocalizedLabel.Label,也就是用户当前使用语言的Label(我在前面的博客中介绍过如何获得当前用户使用的界面语言),如果你想获得其他语言的Label,也可以使用下面的方法:

    private string GetOptionLabel(IOrganizationService service, ITracingService tracingService, string entityname, string attributename, int value, int languagecode) 
    {
    
        RetrieveAttributeRequest retrieveAttributeRequest = 
            new RetrieveAttributeRequest 
            { 
                EntityLogicalName = entityname, 
                LogicalName = attributename 
            };
    
        // Execute the request. 
        RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest); 
        tracingService.Trace("Debug: Entity {0} Attribute {1} is retrieved", entityname, attributename);
    
        OptionMetadataCollection options = ((PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata).OptionSet.Options; 
        OptionMetadata option = options.Where(x => x.Value == value).FirstOrDefault(); 
        tracingService.Trace("Debug: Option of value {0} is retrieved", value);
    
        if (option != null) 
        { 
            tracingService.Trace("Debug: option != null");
    
            var label = option.Label.LocalizedLabels.Where(x => x.LanguageCode == languagecode).FirstOrDefault(); 
            tracingService.Trace("Debug: Label of language {0} is retrieved", languagecode);
    
            if (label != null) 
            { 
                tracingService.Trace("Debug: label != null"); 
                return label.Label; 
            } 
        }
    
        return ""; 
    } 

    这种方法需要对option.Label.LocalizedLabels进行遍历,获得某种语言的Label。

    总结:在前端利用JScript获得某Option Set field的Label或Value很简单,只需要分别使用Xrm.Page.getAttribute(fieldname).getText()和Xrm.Page.getAttribute(fieldname).getValue()。在后端获得Value很容易,因为value值已经包含在OptionSetValue中了,但要获得Label,需要先获得该field的AttributeMetadata,然后对每个option遍历,找到value值相同的option,再返回该option的label。

  • 相关阅读:
    【代码片段】双语导航/导航中鼠标经过变换文字
    【代码片段】渐变写法
    as3.0 复习一
    【代码片段】as3.0 鼠标指针更换代码
    二班信息管理系统
    ubuntu server下apache安装配置以及优化(摘录)
    bootstrap,好东西,对于后端开发人员是个解脱
    今天发现staff邮箱在用LDAP
    FastCGI及PHPFPM
    推荐vi配置及说明
  • 原文地址:https://www.cnblogs.com/jfzhu/p/2813712.html
Copyright © 2020-2023  润新知