Hello Guys,
I am facing a peculiar problem, where I am trying to retrieve the option-sets (Code snippet added below) but I get an error saying "Could not find optionset".
This only happens on production instance but it works fine on the developer instance. I have verified that the option-set I am trying to retrieve is present in that instance and the solution is published.
var retrieveOptionSetRequest = new RetrieveOptionSetRequest() { Name = optionName }; var response = (RetrieveOptionSetResponse)org.Execute(retrieveOptionSetRequest);
Any idea what could have caused this issue?
Thanks
Danish Aalam
*This post is locked for comments
Hello,
I thank all of you for your responses. I figured it yesterday that the option-set was not global. That was the reason I was unable to retrieve them. I made the option-set global and now it works like a charm.
Thanks
Danish Aalam
Here are some methods I use to get option set by label or by value. There is 2 for local options set and 2 for global option set. 1 returns the label and one returns the value based on what you are trying to accomplish. Hope this helps with what you are looking for. If it does please high the verified answer button. Thanks.
public string GetLocalOptionSetLabel(string entityLogicalName, string optionSetName, int value, IOrganizationService service) { XrmServiceContext crmContext = new XrmServiceContext(service); RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest { EntityFilters = EntityFilters.All, LogicalName = entityLogicalName }; RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails); EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata; PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => string.Equals(attribute.LogicalName, optionSetName, StringComparison.OrdinalIgnoreCase)) as PicklistAttributeMetadata; OptionSetMetadata options = picklistMetadata.OptionSet; IList<OptionMetadata> OptionsList = (from o in options.Options where o.Value.Value == value select o).ToList(); string optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Label; return optionsetLabel; }
public string GetGlobalOptionSetLabel(string optionSetName, int value, IOrganizationService service) { XrmServiceContext crmContext = new XrmServiceContext(service); string optionSetLabel = ""; RetrieveOptionSetRequest retrieveOptionSetRequest = new RetrieveOptionSetRequest { Name = optionSetName }; RetrieveOptionSetResponse retrieveOptionSetResponse = (RetrieveOptionSetResponse)crmContext.Execute(retrieveOptionSetRequest); OptionSetMetadata retrievedOptionSetMetadata = (OptionSetMetadata)retrieveOptionSetResponse.OptionSetMetadata; OptionMetadata[] optionArray = retrievedOptionSetMetadata.Options.ToArray(); foreach (var option in optionArray) { if(option.Value.Value == value) { optionSetLabel = option.Label.LocalizedLabels[0].Label; } } return optionSetLabel; }
public int GetLocalOptionSetValue(string entityLogicalName, string optionSetName, string label, IOrganizationService service) { XrmServiceContext crmContext = new XrmServiceContext(service); RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest { EntityFilters = EntityFilters.All, LogicalName = entityLogicalName }; RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails); EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata; PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => string.Equals(attribute.LogicalName, optionSetName, StringComparison.OrdinalIgnoreCase)) as PicklistAttributeMetadata; OptionSetMetadata options = picklistMetadata.OptionSet; IList<OptionMetadata> OptionsList = (from o in options.Options where o.Label.UserLocalizedLabel.Label == label select o).ToList(); int optionsetValue = (OptionsList.First()).Value.Value; return optionsetValue; }
public int GetGlobalOptionSetValue(string optionSetName, string label, IOrganizationService service) { XrmServiceContext crmContext = new XrmServiceContext(service); int optionSetValue = 0; RetrieveOptionSetRequest retrieveOptionSetRequest = new RetrieveOptionSetRequest { Name = optionSetName }; RetrieveOptionSetResponse retrieveOptionSetResponse = (RetrieveOptionSetResponse)crmContext.Execute(retrieveOptionSetRequest); OptionSetMetadata retrievedOptionSetMetadata = (OptionSetMetadata)retrieveOptionSetResponse.OptionSetMetadata; OptionMetadata[] optionArray = retrievedOptionSetMetadata.Options.ToArray(); foreach (var option in optionArray) { if (option.Label.LocalizedLabels[0].Label == label) { optionSetValue = option.Value.Value; } } return optionSetValue; }
Hi,
The RetrieveOptionSetRequest message is used to retrieve a global option sets. The option set you are trying to retrieve is also an global one in the production system?
I am copying the schema name from CRM solution to refer the option-set. The code doesn't work for the out of the box option-sets as well.
I even tried retrieving out of the box option sets like "customertypecode" but unable to retrieve them from this particular instance.
Maybe that option set in production has slightly different schema name(upper case vs lower case, for example)?
Please make sure optionset is from same publisher, it may be it has the same display name but having a different prefix ??
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156