Development Desk

Just another WordPress.com site

Get Option Set Value by Option Set Text in CRM on-prem /Online

To read crm option set information from the CRM, CRM matadata call need to made and it will bring the information on the based of following input attributes. below input attributes are showing test data.
string entityName = "contact";
string fieldName = "title"; // could be any optionset entity attribute like agegroug etc
string optionSetText = "Mr"
pass these values to given method and it will return optionset value of the selected option set.
GetOptionSetValueByText(entityName , attributeName, attributeValue);

public static int? GetOptionSetValueByText(string entityName, string fieldName, string optionSetText)
 {
 var retrieveAttributeRequest = new RetrieveAttributeRequest
 {
 EntityLogicalName = entityName,
 LogicalName = fieldName,
 RetrieveAsIfPublished = true
 };

 var retrieveAttributeResponse = (RetrieveAttributeResponse)_orgService.Execute(retrieveAttributeRequest);
 var attributeMetadataList = (EnumAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;

 var optionSetInfo = (from optionSetDetail in attributeMetadataList.OptionSet.Options
 from labelInfo in optionSetDetail.Label.LocalizedLabels.Where(label => label.Label == optionSetText).Where(l => optionSetDetail.Value != null)
 select optionSetDetail).ToList();

 return optionSetInfo.Any() && optionSetInfo.Count > 0 && optionSetInfo[0].Value.HasValue ? optionSetInfo[0].Value : null;
 }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: