獲取Dynamic CRM選項集類型(OptionSet)字段的文本(label)值


前端(Js)

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

后端(C#)

 public static string GetOptionsSetTextOnValue(IOrganizationService service, string entityName, string attributeName, int selectedValue)
    {

        RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest
        {
            EntityLogicalName = entityName,
            LogicalName = attributeName,
            RetrieveAsIfPublished = true
        };

        RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);

        Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata retrievedPicklistAttributeMetadata = (Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata)

        retrieveAttributeResponse.AttributeMetadata;
        OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
        string selectedOptionLabel = string.Empty;
        foreach (OptionMetadata oMD in optionList)
        {
            if (oMD.Value == selectedValue)
            {
                selectedOptionLabel = oMD.Label.UserLocalizedLabel.Label;
            }
        }
        return selectedOptionLabel;
    }

正常情況下我們獲取到的都是Value值,通過上述代碼就可以獲取到Value對應的Label值。


免責聲明!

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



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