Simply build a new RetrieveAttributeRequest and iterate over the response like so:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var attributeRequest = new RetrieveAttributeRequest | |
{ | |
EntityLogicalName = "entityName", | |
LogicalName = "attributeName", | |
RetrieveAsIfPublished = true | |
}; | |
var attributeResponse = (RetrieveAttributeResponse)service.Execute(attributeRequest); | |
var attributeMetadata = (EnumAttributeMetadata)attributeResponse.AttributeMetadata; | |
var optionList = (from o in attributeMetadata.OptionSet.Options | |
select new { Value = o.Value, Text = o.Label.UserLocalizedLabel.Label }).ToList(); |
optionList now contains a list of all option set options where each object in the list has a Value property and a Text property.
To retrieve a given option set value for a label, use a Linq query:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var activeValue = optionList.Where(o => o.Text == "Active") | |
.Select(o => o.Value) | |
.FirstOrDefault(); |
What is Active here It is giving me null value?
ReplyDeleteGreat content. To the point and addressing an issue that few others have shared a solution for.
ReplyDelete