Wednesday, 18 September 2013

Dynamics CRM 2011 Sorting Options sets alphabetically

I was recently working with a custom options set solution which renders options sets as multi select picklists, the solution is here:

I came across a rather unpleasant reality on CRM options sets which is doing the right thing however causing a bit of a maintenance overhead if you want to extend CRM into other areas.

if we retrieve an option set with the below code:
returns an array of objects which may look like this:

Notice the sorting is done on the index value. Imagine you have 50 or more entries in one option set and once in a while you need to add a new entry and remove others, are you manually moving all index values to sort the option set alphabetically? if you have another entry starting with letter "A" then you need to change all none "A" to + 1 in the index value.
This would be painful and time consuming. I'm sharing a small piece of code that can do this for you if you face the same issue:

The results would then be sorted alphabetically:

Notice the index values now are not sequential and the text entries are alphabetically sorted.

Hope this was helpful, please leave your feedback.


  1. Hi, thank you for the code! iworks fine !!! there is only one problem: it gives back the first line empty, so i have the first option without description.
    How can I revise it ?

  2. I solved it by checking the text !=null
    thank you !!!!!

    1. Hi Massimiliano,
      Thanks for the feedback... The first line empty I believe is browser related it happens on IE8 though IE9 is okay thanks for sharing your solution as well.


    2. Hi, I got the same problem, on IE 8 the first line is empty and the first label is on the second checkbox. I think it is an incompability problem. Have you got an idea to change it? Thank you very much

    3. Hi Celine,
      its an IE8 issue, Massimiliano has provided a workaround for that, add a check for: text !=null and this will fix the issue with the blank line.
      Hope this helps