The ‘co’ (country) attribute in Active Directory is a strange property… You cannot just set it to some value and be done with it. This attribute has two partners in crime and they are picky about the stuff you throw at them!

The attributes I’m talking about are:

  • co
  • c
  • countryCode

These attributes must be set together at the same time with specific values for them to be stored in Active Directory.

Let’s look at the values you are allowed to use for these properties.. yep, you will need the ISO 3166-1. My first guess was using the CultureInfo and RegionInfo objects that are available in the .NET Framework, but beware! The list of cultures in .NET is incomplete and you cannot get the numeric code from the RegionInfo either.

So we need to set the name of a country, its two-letter name, and some cryptic code all at once. ‘co’ needs the full name of a country, ‘c’ needs a two-letter countrycode and the ‘countryCode’ requires the numeric code. Below you will find some code that has all that stuff ready-made so you don’t have to! All you need to do now is have your code find a country and update a DirectoryEntry.

Here is the code that you can adjust to your needs for the taking! The code has all the countries that are on the ISO 3166-1 list. Feel free to leave a comment if you have an improvement on this code!

Use it any way you want, happy copy-pasting

public class CountryData
{
public string Name { get; private set; }
public string TwoLetterCode { get; private set; }
public int NumericCode { get; private set; }

public CountryData(string fullName, string twoLetterCode, int numericCode)
{
Name = fullName;
TwoLetterCode = twoLetterCode;
NumericCode = numericCode;
}
}

public class CountryInfo
{
public readonly ICollection Countries;

public CountryInfo()
{
Countries = new List
{
{ new CountryData("Afghanistan", "AF", 4) },
{ new CountryData("Åland Islands", "AX", 248) },
{ new CountryData("Albania", "AL", 8) },
{ new CountryData("Algeria", "DZ", 12) },
{ new CountryData("American Samoa", "AS", 16) },
{ new CountryData("Andorra", "AD", 20) },
{ new CountryData("Angola", "AO", 24) },
{ new CountryData("Anguilla", "AI", 660) },
{ new CountryData("Antarctica", "AQ", 10) },
{ new CountryData("Antigua and Barbuda", "AG", 28) },
{ new CountryData("Argentina", "AR", 32) },
{ new CountryData("Armenia", "AM", 51) },
{ new CountryData("Aruba", "AW", 533) },
{ new CountryData("Australia", "AU", 36) },
{ new CountryData("Austria", "AT", 40) },
{ new CountryData("Azerbaijan", "AZ", 31) },
{ new CountryData("Bahamas", "BS", 44) },
{ new CountryData("Bahrain", "BH", 48) },
{ new CountryData("Bangladesh", "BD", 50) },
{ new CountryData("Barbados", "BB", 52) },
{ new CountryData("Belarus", "BY", 112) },
{ new CountryData("Belgium", "BE", 56) },
{ new CountryData("Belize", "BZ", 84) },
{ new CountryData("Benin", "BJ", 204) },
{ new CountryData("Bermuda", "BM", 60) },
{ new CountryData("Bhutan", "BT", 64) },
{ new CountryData("Bolivia, Plurinational State of", "BO", 68) },
{ new CountryData("Bonaire, Sint Eustatius and Saba", "BQ", 535) },
{ new CountryData("Bosnia and Herzegovina", "BA", 70) },
{ new CountryData("Botswana", "BW", 72) },
{ new CountryData("Bouvet Island", "BV", 74) },
{ new CountryData("Brazil", "BR", 76) },
{ new CountryData("British Indian Ocean Territory", "IO", 86) },
{ new CountryData("Brunei Darussalam", "BN", 96) },
{ new CountryData("Bulgaria", "BG", 100) },
{ new CountryData("Burkina Faso", "BF", 854) },
{ new CountryData("Burundi", "BI", 108) },
{ new CountryData("Cambodia", "KH", 116) },
{ new CountryData("Cameroon", "CM", 120) },
{ new CountryData("Canada", "CA", 124) },
{ new CountryData("Cape Verde", "CV", 132) },
{ new CountryData("Cayman Islands", "KY", 136) },
{ new CountryData("Central African Republic", "CF", 140) },
{ new CountryData("Chad", "TD", 148) },
{ new CountryData("Chile", "CL", 152) },
{ new CountryData("China", "CN", 156) },
{ new CountryData("Christmas Island", "CX", 162) },
{ new CountryData("Cocos (Keeling) Islands", "CC", 166) },
{ new CountryData("Colombia", "CO", 170) },
{ new CountryData("Comoros", "KM", 174) },
{ new CountryData("Congo", "CG", 178) },
{ new CountryData("Congo, the Democratic Republic of the", "CD", 180) },
{ new CountryData("Cook Islands", "CK", 184) },
{ new CountryData("Costa Rica", "CR", 188) },
{ new CountryData("C“te d'Ivoire", "CI", 384) },
{ new CountryData("Croatia", "HR", 191) },
{ new CountryData("Cuba", "CU", 192) },
{ new CountryData("Cura‡ao", "CW", 531) },
{ new CountryData("Cyprus", "CY", 196) },
{ new CountryData("Czech Republic", "CZ", 203) },
{ new CountryData("Denmark", "DK", 208) },
{ new CountryData("Djibouti", "DJ", 262) },
{ new CountryData("Dominica", "DM", 212) },
{ new CountryData("Dominican Republic", "DO", 214) },
{ new CountryData("Ecuador", "EC", 218) },
{ new CountryData("Egypt", "EG", 818) },
{ new CountryData("El Salvador", "SV", 222) },
{ new CountryData("Equatorial Guinea", "GQ", 226) },
{ new CountryData("Eritrea", "ER", 232) },
{ new CountryData("Estonia", "EE", 233) },
{ new CountryData("Ethiopia", "ET", 231) },
{ new CountryData("Falkland Islands (Malvinas)", "FK", 238) },
{ new CountryData("Faroe Islands", "FO", 234) },
{ new CountryData("Fiji", "FJ", 242) },
{ new CountryData("Finland", "FI", 246) },
{ new CountryData("France", "FR", 250) },
{ new CountryData("French Guiana", "GF", 254) },
{ new CountryData("French Polynesia", "PF", 258) },
{ new CountryData("French Southern Territories", "TF", 260) },
{ new CountryData("Gabon", "GA", 266) },
{ new CountryData("Gambia", "GM", 270) },
{ new CountryData("Georgia", "GE", 268) },
{ new CountryData("Germany", "DE", 276) },
{ new CountryData("Ghana", "GH", 288) },
{ new CountryData("Gibraltar", "GI", 292) },
{ new CountryData("Greece", "GR", 300) },
{ new CountryData("Greenland", "GL", 304) },
{ new CountryData("Grenada", "GD", 308) },
{ new CountryData("Guadeloupe", "GP", 312) },
{ new CountryData("Guam", "GU", 316) },
{ new CountryData("Guatemala", "GT", 320) },
{ new CountryData("Guernsey", "GG", 831) },
{ new CountryData("Guinea", "GN", 324) },
{ new CountryData("Guinea-Bissau", "GW", 624) },
{ new CountryData("Guyana", "GY", 328) },
{ new CountryData("Haiti", "HT", 332) },
{ new CountryData("Heard Island and McDonald Islands", "HM", 334) },
{ new CountryData("Holy See (Vatican City State)", "VA", 336) },
{ new CountryData("Honduras", "HN", 340) },
{ new CountryData("Hong Kong", "HK", 344) },
{ new CountryData("Hungary", "HU", 348) },
{ new CountryData("Iceland", "IS", 352) },
{ new CountryData("India", "IN", 356) },
{ new CountryData("Indonesia", "ID", 360) },
{ new CountryData("Iran, Islamic Republic of", "IR", 364) },
{ new CountryData("Iraq", "IQ", 368) },
{ new CountryData("Ireland", "IE", 372) },
{ new CountryData("Isle of Man", "IM", 833) },
{ new CountryData("Israel", "IL", 376) },
{ new CountryData("Italy", "IT", 380) },
{ new CountryData("Jamaica", "JM", 388) },
{ new CountryData("Japan", "JP", 392) },
{ new CountryData("Jersey", "JE", 832) },
{ new CountryData("Jordan", "JO", 400) },
{ new CountryData("Kazakhstan", "KZ", 398) },
{ new CountryData("Kenya", "KE", 404) },
{ new CountryData("Kiribati", "KI", 296) },
{ new CountryData("Korea, Democratic People's Republic of", "KP", 408) },
{ new CountryData("Korea, Republic of", "KR", 410) },
{ new CountryData("Kuwait", "KW", 414) },
{ new CountryData("Kyrgyzstan", "KG", 417) },
{ new CountryData("Lao People's Democratic Republic", "LA", 418) },
{ new CountryData("Latvia", "LV", 428) },
{ new CountryData("Lebanon", "LB", 422) },
{ new CountryData("Lesotho", "LS", 426) },
{ new CountryData("Liberia", "LR", 430) },
{ new CountryData("Libya", "LY", 434) },
{ new CountryData("Liechtenstein", "LI", 438) },
{ new CountryData("Lithuania", "LT", 440) },
{ new CountryData("Luxembourg", "LU", 442) },
{ new CountryData("Macao", "MO", 446) },
{ new CountryData("Macedonia, the former Yugoslav Republic of", "MK", 807) },
{ new CountryData("Madagascar", "MG", 450) },
{ new CountryData("Malawi", "MW", 454) },
{ new CountryData("Malaysia", "MY", 458) },
{ new CountryData("Maldives", "MV", 462) },
{ new CountryData("Mali", "ML", 466) },
{ new CountryData("Malta", "MT", 470) },
{ new CountryData("Marshall Islands", "MH", 584) },
{ new CountryData("Martinique", "MQ", 474) },
{ new CountryData("Mauritania", "MR", 478) },
{ new CountryData("Mauritius", "MU", 480) },
{ new CountryData("Mayotte", "YT", 175) },
{ new CountryData("Mexico", "MX", 484) },
{ new CountryData("Micronesia, Federated States of", "FM", 583) },
{ new CountryData("Moldova, Republic of", "MD", 498) },
{ new CountryData("Monaco", "MC", 492) },
{ new CountryData("Mongolia", "MN", 496) },
{ new CountryData("Montenegro", "ME", 499) },
{ new CountryData("Montserrat", "MS", 500) },
{ new CountryData("Morocco", "MA", 504) },
{ new CountryData("Mozambique", "MZ", 508) },
{ new CountryData("Myanmar", "MM", 104) },
{ new CountryData("Namibia", "NA", 516) },
{ new CountryData("Nauru", "NR", 520) },
{ new CountryData("Nepal", "NP", 524) },
{ new CountryData("Netherlands", "NL", 528) },
{ new CountryData("New Caledonia", "NC", 540) },
{ new CountryData("New Zealand", "NZ", 554) },
{ new CountryData("Nicaragua", "NI", 558) },
{ new CountryData("Niger", "NE", 562) },
{ new CountryData("Nigeria", "NG", 566) },
{ new CountryData("Niue", "NU", 570) },
{ new CountryData("Norfolk Island", "NF", 574) },
{ new CountryData("Northern Mariana Islands", "MP", 580) },
{ new CountryData("Norway", "NO", 578) },
{ new CountryData("Oman", "OM", 512) },
{ new CountryData("Pakistan", "PK", 586) },
{ new CountryData("Palau", "PW", 585) },
{ new CountryData("Palestinian Territory, Occupied", "PS", 275) },
{ new CountryData("Panama", "PA", 591) },
{ new CountryData("Papua New Guinea", "PG", 598) },
{ new CountryData("Paraguay", "PY", 600) },
{ new CountryData("Peru", "PE", 604) },
{ new CountryData("Philippines", "PH", 608) },
{ new CountryData("Pitcairn", "PN", 612) },
{ new CountryData("Poland", "PL", 616) },
{ new CountryData("Portugal", "PT", 620) },
{ new CountryData("Puerto Rico", "PR", 630) },
{ new CountryData("Qatar", "QA", 634) },
{ new CountryData("R‚union", "RE", 638) },
{ new CountryData("Romania", "RO", 642) },
{ new CountryData("Russian Federation", "RU", 643) },
{ new CountryData("Rwanda", "RW", 646) },
{ new CountryData("Saint Barth‚lemy", "BL", 652) },
{ new CountryData("Saint Helena, Ascension and Tristan da Cunha", "SH", 654) },
{ new CountryData("Saint Kitts and Nevis", "KN", 659) },
{ new CountryData("Saint Lucia", "LC", 662) },
{ new CountryData("Saint Martin (French part)", "MF", 663) },
{ new CountryData("Saint Pierre and Miquelon", "PM", 666) },
{ new CountryData("Saint Vincent and the Grenadines", "VC", 670) },
{ new CountryData("Samoa", "WS", 882) },
{ new CountryData("San Marino", "SM", 674) },
{ new CountryData("Sao Tome and Principe", "ST", 678) },
{ new CountryData("Saudi Arabia", "SA", 682) },
{ new CountryData("Senegal", "SN", 686) },
{ new CountryData("Serbia", "RS", 688) },
{ new CountryData("Seychelles", "SC", 690) },
{ new CountryData("Sierra Leone", "SL", 694) },
{ new CountryData("Singapore", "SG", 702) },
{ new CountryData("Sint Maarten (Dutch part)", "SX", 534) },
{ new CountryData("Slovakia", "SK", 703) },
{ new CountryData("Slovenia", "SI", 705) },
{ new CountryData("Solomon Islands", "SB", 90) },
{ new CountryData("Somalia", "SO", 706) },
{ new CountryData("South Africa", "ZA", 710) },
{ new CountryData("South Georgia and the South Sandwich Islands", "GS", 239) },
{ new CountryData("South Sudan", "SS", 728) },
{ new CountryData("Spain", "ES", 724) },
{ new CountryData("Sri Lanka", "LK", 144) },
{ new CountryData("Sudan", "SD", 729) },
{ new CountryData("Suriname", "SR", 740) },
{ new CountryData("Svalbard and Jan Mayen", "SJ", 744) },
{ new CountryData("Swaziland", "SZ", 748) },
{ new CountryData("Sweden", "SE", 752) },
{ new CountryData("Switzerland", "CH", 756) },
{ new CountryData("Syrian Arab Republic", "SY", 760) },
{ new CountryData("Taiwan, Province of China", "TW", 158) },
{ new CountryData("Tajikistan", "TJ", 762) },
{ new CountryData("Tanzania, United Republic of", "TZ", 834) },
{ new CountryData("Thailand", "TH", 764) },
{ new CountryData("Timor-Leste", "TL", 626) },
{ new CountryData("Togo", "TG", 768) },
{ new CountryData("Tokelau", "TK", 772) },
{ new CountryData("Tonga", "TO", 776) },
{ new CountryData("Trinidad and Tobago", "TT", 780) },
{ new CountryData("Tunisia", "TN", 788) },
{ new CountryData("Turkey", "TR", 792) },
{ new CountryData("Turkmenistan", "TM", 795) },
{ new CountryData("Turks and Caicos Islands", "TC", 796) },
{ new CountryData("Tuvalu", "TV", 798) },
{ new CountryData("Uganda", "UG", 800) },
{ new CountryData("Ukraine", "UA", 804) },
{ new CountryData("United Arab Emirates", "AE", 784) },
{ new CountryData("United Kingdom", "GB", 826) },
{ new CountryData("United States", "US", 840) },
{ new CountryData("United States Minor Outlying Islands", "UM", 581) },
{ new CountryData("Uruguay", "UY", 858) },
{ new CountryData("Uzbekistan", "UZ", 860) },
{ new CountryData("Vanuatu", "VU", 548) },
{ new CountryData("Venezuela, Bolivarian Republic of", "VE", 862) },
{ new CountryData("Viet Nam", "VN", 704) },
{ new CountryData("Virgin Islands, British", "VG", 92) },
{ new CountryData("Virgin Islands, U.S.", "VI", 850) },
{ new CountryData("Wallis and Futuna", "WF", 876) },
{ new CountryData("Western Sahara", "EH", 732) },
{ new CountryData("Yemen", "YE", 887) },
{ new CountryData("Zambia", "ZM", 894) },
{ new CountryData("Zimbabwe", "ZW", 716) }
};
}

public CountryData GetCountryByNumericCode(int code)
{
var data = Countries.FirstOrDefault(cd => cd.NumericCode == code);

if (data == null)
throw new ArgumentException(string.Format("{0} is an invalid numeric countrycode!", code), "code");

return data;
}

public CountryData GetCountryByNameOrCode(string nameOrTwoLetterCode)
{
var data = Countries.FirstOrDefault(cd => string.Equals(cd.Name, nameOrTwoLetterCode, StringComparison.InvariantCultureIgnoreCase) || string.Equals(cd.TwoLetterCode, nameOrTwoLetterCode, StringComparison.InvariantCultureIgnoreCase));

if (data == null)
throw new ArgumentException(string.Format("CountryData with name or two letter code {0} does not exist!", nameOrTwoLetterCode), "nameOrTwoLetterCode");

return data;
}
}

Related articles

  • Cloud Native
  • Application Navigator
  • Kubernetes Platform
  • Digital Workspace
  • Cloud Infrastructure
  • ITTS (IT Transformation Services)
  • Managed Security Operations
  • Multi-Cloud Platform
  • Backup & Disaster Recovery
Visit our knowledge hub
Visit our knowledge hub
Sander Harrewijnen Developer

Let's talk!

Knowledge is key for our existence. This knowledge we use for disruptive innovation and changing organizations. Are you ready for change?

"*" indicates required fields

First name*
Last name*
Hidden