Skip to main content

Option Set Helper Code

When it comes to working with Option Sets while creating a plugin, custom workflow activity or other extension to CRM, it can sometimes be a pain to get the label of the Option Set.  There are many solutions out there on how to accomplish this, but for the most part they were not reliable.  So after doing a bunch of research online and going through the SDK I came up with 4 methods that help me when I am working with Option Sets.  There are 2 for global option sets and 2 for local option sets.  The reason there are 2 for each is because one method will return the value and the other will return the label.

1) Get Local Option Set Label - This method will return the label of the option set value you provide it.
     /// <summary>  
     /// Method to get local option set label  
     /// </summary>  
     /// <param name="entityLogicalName">Schema name of entity</param>  
     /// <param name="optionSetName">schema name of the optionset</param>  
     /// <param name="value">value of the option set option</param>  
     /// <param name="service">CRM Organizational Service</param>  
     /// <returns>String Label of the optionset value provided</returns>  
     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;  
     } 

2) Get Local Option Set Value - This method till return the value of the option set label you pass it.
     /// <summary>  
     /// Method to get local option set value  
     /// </summary>  
     /// <param name="entityLogicalName">Schema Name of entity</param>  
     /// <param name="optionSetName">Schema Name of option set.</param>  
     /// <param name="label">text label</param>  
     /// <param name="service">CRM Org Service</param>  
     /// <returns>Int option set value</returns>  
     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;  
     }  

3) Get Global Option Set Label - This method will return the label of a global option set value you pass it.
         /// <summary>  
     /// Method to get global option set label  
     /// </summary>  
     /// <param name="optionSetName">Schema name of option set</param>  
     /// <param name="value">Int value of option set value</param>  
     /// <param name="service">CRM Org service</param>  
     /// <returns>String option set label</returns>  
     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;  
     }  

4) Get Global Option Set Value - This method will return the value of the global option set label you pass it.
     /// <summary>  
     /// Method to get the option set value for a global optionset  
     /// </summary>  
     /// <param name="optionSetName">Schema name of the optionset</param>  
     /// <param name="label">text label</param>  
     /// <param name="service">CRM Org Service</param>  
     /// <returns>Int option set value</returns>  
     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;  
     }  

Comments

Popular posts from this blog

Dynamics 365 v9 Unit Testing and .NET Confusion

Recently while creating a plugin for Dynamics 365 v9, I ran into an issue trying to connect to CRM via the tooling connector in my unit test project.  The underlying problem was that the .NET version I was using in my unit test was 4.5.2.  This was preventing me from being able to connect to CRM to create my organization service.  I updated the .NET version on my unit test project to 4.6.1 and was then finally able to connect.  I will also add that I am using the latest nuget package version for Dynamics 365 v9.

For consistence, I updated the plugin project I was working on to .NET 4.6.1.  Locally, everything was working great.  I was able to connect to CRM and make sure that all the methods I had written did what they where suppose to do using test driven development practices.

Then when publishing my plugin via the latest version of the plugin registration tool, I received an error and could not publish my plugin.  The error was due to the .NET version of my plugin project not bein…

SCRIBE Connector Development - Handling Array List

Are you working on creating a connector with SCRIBE's CDK?  In your connector do you have an array of strings or list of  strings that you need to pass?  SCRIBE makes this easy to do within the CDK and SCRIBE Online.

I came across this scenario on a connector I was creating that passes a JSON message to an API.  In the JSON message it had a list of strings for entity ID's. Here is an easy way to accomplish this:

1) Create you Property Definition as past or your Object Definition.
1: new PropertyDefinition 2: { 3: Description = "Use TOLIST() to pass in a list of entity id's.", 4: FullName = "Entity IDs", 5: IsPrimaryKey = false, 6: MaxOccurs = 1, 7: MinOccurs = 0, 8: Name = "PublishTo", 9: Nullable = true, 10: NumericPrecision = 0, 11: NumericScale = 0, 12: PresentationType = "string", 13: PropertyType = typeof(string).Name, 14: Use…

Getting Started Connector Development

One of the benefits of working with Scribe Online is how easy they make it to create connectors if one does not exist.  In this blog post we are going to look at how to get setup, if this is the first time you have made a connector for Scribe Online.  But, before we get into that, we should first make sure that a connector doesn't already exist that can handle what we need.

We can do this by looking in the Scribe Online Marketplace.  Still not seeing what you need?  Reach out to Scribe directly or ask in the Scribe forums if a connector exists for an application.  There are instances where a connector exists but is not listed in the marketplace.  An example of this is for a client that I built a connector for.  They didn't want to setup a system to run the on-premise agent, so they asked me to set up the connector to run on Scribe's cloud agent.  This meant that I had to submit the connector to Scribe for validation.  Once published the connector is in the Scribe marketpla…