前端(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值。