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。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM