Import a Microsoft CRM 2015 Global Option Set with C#

After many frustrated google searches, I have been unable to find an out of the box solution for importing option sets into a Microsoft CRM 2015 environment. There are a few articles out there about importing records with the values, then deleting the records, but this does not allow you to control the numeric value of the import. Looking at the hours of mindless manual entry that was staring me in the face for a client demo, I made one more search into MSDN in hopes that I could put together a simple piece of code that might be able to do it. Luckily enough I found that Microsoft has included a simple code example in the SDK that shows how to work with option sets programmatically (https://msdn.microsoft.com/en-us/library/gg509023(v=crm.7).aspx). With a little work I was able to come up with a generic re-usable function that can be passed a dictionary and turn it into a global option set.

Here is what the full function looks like:

public static CreateOptionSetResponse CreateGlobalOptionSet(IOrganizationService organizationService
                                                        , string logicalName
                                                        , string dispalyName
                                                        , Dictionary<int, string> optionDictionary
                                                        ,int languageCode = 1033)
{
    //Create and configure the new optionset.
    OptionSetMetadata newOptionSet = new OptionSetMetadata();
    newOptionSet.Name = logicalName;
    newOptionSet.DisplayName = new Label(dispalyName,languageCode);
    newOptionSet.IsGlobal = true;
    newOptionSet.OptionSetType = OptionSetType.Picklist;

    //add options to the optionset
    foreach(KeyValuePair<int,string> item in optionDictionary)
    { 
        newOptionSet.Options.Add(
            new OptionMetadata(new Label(item.Value,languageCode),
                                item.Key)
            );
    }
    
    //Create the request object
    CreateOptionSetRequest createOptionSetRequest = new CreateOptionSetRequest 
    {
        //Add the new optionset to the request
        OptionSet = newOptionSet
    };
    
    //execute the request
    CreateOptionSetResponse optionResponse = 
        (CreateOptionSetResponse)organizationService.Execute(createOptionSetRequest);
        
    //return the response
    return optionResponse;
}

All you need to provide:

  • Your crm connection
  • The logical name you would want for the option set
  • The display name for the option set
  • A dictionary<int, string> of the keys and values you want the option set to contain
  • Your language code value (it defaults to 1033 – English)

Just use your preferred method of loading the data into the dictionary and you are good to go.

 

One thought on “Import a Microsoft CRM 2015 Global Option Set with C#

  1. WoW, fantastic… This very very help me. I needed to create a picklist of more 5000 items. Thank you very much.

Leave a Reply

Your email address will not be published.