How to Quickly Extract an Option Set to a Text File

Every so often, you need to get a dump of all the labels and values of an Option Set from Dynamics 365 Customer Engagement (aka CRM). If the Option Set only has a few options this can be done by hand very quickly. However, for larger lists, we need to get creative.

To do this quickly and efficiently, we will simply export the Option Set in a solution with no other components then use a text editor and a regular expression to transform the list into a more usable format.

Let’s consider the following example Option Set, which lists all the types of Trees in Canada:

As we can see, this has over 300 options in it. There’s no way we’re going to print these by hand.

To start, simply create a new blank solution then use “Add Existing” to include the target Option Set. Note that this method will also work for multiple Option Sets at once, simply include each Option Set you want extracted in the solution.

Then export the solution as an unmanaged solution. You can delete the temporary solution after the download is complete, this will not delete the Option Sets themselves.

Solutions are simply .zip files. You can extract the contents using Windows Explorer or any other standard file browsing tool. Inside the extracted files, find the “customzations.xml” file. It will look like the following:

The thing here to notice, is that the options here are all listed using the following XML format (note that depending on translations and language options your syntax might looks slightly different):

<option value="optionValue" ExternalValue="" Color="#0000ff">
  <labels>
    <label description="optionLabel" languagecode="1033" />
  </labels>
  <Descriptions>
    <Description description="" languagecode="1033" />
  </Descriptions>
</option>

We can take advantage of this, by using a regular expression to boil all of that XML down into a simple value and label. If you’re using a text editor such as Sublime Text, you can simple highlight the <option> node and press Ctrl+H to open the Find & Replace window. You can then replace the relevant parts with regex syntax, like so:

The leading ^\s* will get rid of any whitespace at the start of the line. The (\d*) defines the first capture group to get the value, then replace the label with (.*) to store it as the second capture group. Conveniently, nothing in the normal XML structure has special meaning in regex so does not need escaping. The replace text \1\t\2 will output the value of the first capture group, a tab, and then the value of the second capture group.

Then click “Replace All” and voilà:

Finally, strip the header and footer XML bits and then you’re left with a simple text file of values and labels.

If you want a different format, simply change the replace text, for example \2,\1 for the label, then the value in CSV format.