Dynamics CRM: Add Items to an OptionSet

Printer-friendly version

While working in Dynamics CRM, I needed to add several options to a few different optionsets on the Lead entity. Adding the options isn’t difficult, but can be tedious and repetitive, so I didn’t want to do it manually. I decided to leveraged the CRM SDK and used a LINQPad query to populate the optionset as shown in Figure 1.

First, given that an optionset is a list of name value pairs, I created a simple class with a Label and Value attribute.

class OptionSetItem


    public string Label { get; set; }

    public int Value { get; set; }


Next, I used a helper method to create a collection of the OptionSetItems that I wanted to insert. These values could have been retrieved from data store such as a file, service or database.

IEnumerable<OptionSetItem> GetOptionSetItems()


    // Items to add

    var items = new List<OptionSetItem> {

        new OptionSetItem {Label="1-249", Value=300000003},

        new OptionSetItem {Label="250-499", Value=300000004},

        new OptionSetItem {Label="500-749", Value=300000005},

        new OptionSetItem {Label="750-999", Value=300000006},

        new OptionSetItem {Label="1000-1499", Value=300000007},

        new OptionSetItem {Label="1500-1999", Value=300000008},

        new OptionSetItem {Label="2000+", Value=300000009},

        new OptionSetItem {Label="Unknown", Value=300000010}


    return items;


Then I added a method that uses the InsertOptionValueRequest class to add a given option by specifying the Entity, OptionSet Fieldname, OptionSet Label and OptionSet Value as follows. Note, for the label I am using the language code for English (1033). For other languages, see the Microsoft Locale ID Chart.

int AddOptionSetItem(string entityName, string fieldName, string optionLabel, int optionValue, IOrganizationService service)


    // Create the Insert request

    var insertOptionValueRequest = new InsertOptionValueRequest


        AttributeLogicalName = fieldName,

        EntityLogicalName = entityName,

        Label = new Label(optionLabel, 1033),

        Value = optionValue     // CRM defaulted, if not specified



    // Execute the request

    return ((InsertOptionValueResponse)service.Execute(insertOptionValueRequest)).NewOptionValue;


Lastly, in the main method, I added logic to connect to CRM, loop through my collection of optionset values and add the options to the specified optionset in on the Lead entity in CRM. The newly added options are shown in Figure 1.

void Main()


    var connectionString = ConfigurationManager.ConnectionStrings["dev"].ConnectionString;    

    using (var client = new CrmServiceClient(connectionString))


        var service = (IOrganizationService)client.OrganizationServiceProxy;        

        using (var context = new OrganizationServiceContext(service))


            foreach (var item in GetOptionSetItems())


                AddOptionSetItem("lead", "hw_categorysize", item.Label, item.Value, service)

                .Dump(item.Label + " added...");





As you can see it’s relatively simple with just a few lines of code and can be reused as needed. The options can be removed from an optionset as well using the DeleteOptionValueRequest class, and you can also create a new optionset if needed using the OptionSetMetadata class. Be sure to review the Dynamics CRM SDK for more information.


Figure 1. OptionSet with Values added


About the Author:

TopLine Strategies delivers the complete integration and development of sales, marketing and customer service technologies that enable corporate clientele to improve revenue streams and strengthen customer interactions. Our project management and consulting is designed to achieve timely delivery, 100 percent user adoption of the technologies we implement and deliver measurable returns on investments for our clients.

Comments (0)

Related Blogs

TheReact Native Open Source roadmap was announced in Q4 2018 after they decided to invest more in the React Native open source community.

October is not just about pumpkins, fall foliage, and cooler temps anymore. October 2018 also means the exciting introduction of Microsoft Dynamics 365 for Customer Engagement.

Back in 2016, Microsoft introduced its intentions to refresh its CRM and ERP strategy with Dynamics 365. At the heart of its services was the Common Data Model (CDM).