CRM 2011 Get OptionSetValue Label Text
In CRM 2011, when you try to get the value of an OptionSet, you will always get an Integer value instead of the label value.
I found out that you have to use RetrieveAttributeRequest and get the OptionMetadata in order to get the label text of a given value. This is intended so that it supports multiple languages.
Therefore I created a service extension method in order to get the label from the OptionSet:
To use this you just need to pass in the Entity, the OptionSet attribute you're looking for, and the Option Value you want to get the label from:
Example (early bound):
Hope this helps,
Andreas
I found out that you have to use RetrieveAttributeRequest and get the OptionMetadata in order to get the label text of a given value. This is intended so that it supports multiple languages.
Therefore I created a service extension method in order to get the label from the OptionSet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public static string GetOptionSetValueLabel( this IOrganizationService service, Entity entity, string attribute, OptionSetValue option) { string optionLabel = String.Empty; RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest { EntityLogicalName = entity.LogicalName, LogicalName = attribute, RetrieveAsIfPublished = true }; RetrieveAttributeResponse attributeResponse = (RetrieveAttributeResponse)service.Execute(attributeRequest); AttributeMetadata attrMetadata = (AttributeMetadata)attributeResponse.AttributeMetadata; PicklistAttributeMetadata picklistMetadata = (PicklistAttributeMetadata)attrMetadata; // For every status code value within all of our status codes values // (all of the values in the drop down list) foreach (OptionMetadata optionMeta in picklistMetadata.OptionSet.Options) { // Check to see if our current value matches if (optionMeta.Value == option.Value) { // If our numeric value matches, set the string to our status code // label optionLabel = optionMeta.Label.UserLocalizedLabel.Label; } } return optionLabel; } |
To use this you just need to pass in the Entity, the OptionSet attribute you're looking for, and the Option Value you want to get the label from:
Example (early bound):
1 | service.GetOptionSetValueLabel(caseEntity, "casetypecode" , caseEntity.CaseTypeCode) |
Hope this helps,
Andreas
very useful
ReplyDeletethanks :)