Get OptionSetValue Label using RetrieveAttributeRequest and PicklistAttributeMetadata


Sometimes, we get the requirement where we need to get the OptionSetValue label in the plugin/workflow/some program.
We can get that label by using RetrieveAttributeRequest and PicklistAttributeMetadata.

Here is the C# code for it.

private string GetCRMOptionSetValueLabel(IOrganizationService objService, string strEntityName, string strOptionSetName, int intOptionSetValue)
            RetrieveAttributeRequest reqOptionSet = new RetrieveAttributeRequest();
            reqOptionSet.EntityLogicalName = strEntityName;
            reqOptionSet.LogicalName = strOptionSetName;
            RetrieveAttributeResponse resp = (RetrieveAttributeResponse)objService.Execute(reqOptionSet);
            PicklistAttributeMetadata opdata = (PicklistAttributeMetadata)resp.AttributeMetadata;
            var option = opdata.OptionSet.Options.FirstOrDefault(o => o.Value == intOptionSetValue);
            return option.Label.LocalizedLabels.FirstOrDefault().Label;

Hope this helps.

Happy CRM’ing

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s