Wednesday, 20 May 2015

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

No comments:

Post a Comment