Sort coded value domains ArcGIS Online

3302
5
Jump to solution
04-01-2019 04:03 AM
Kristian_BirchvaldJensen
New Contributor

Hello

Is it possible to sort a list of coded value domains in ArcGIS Online?

I have a hosted Feature Layer with a field with domains. When I view them in ArcGIS Online they are not sorted by the Code value. I would like them to be sorted by code value, like using the tool Sort coded value domain in ArcGIS Desktop.

Since I couldn't find any standard way of doing this I have tried to use ArcGIS API for Python in Jupyter Notebook, and the arcpy.SortCodedValueDomain_management tool but It returns an error saying my hosted feature layer is not a valid geodatabase (see attached photo - with deliberately wrong user and password).

Perhaps someone can help me here?

1 Solution

Accepted Solutions
Egge-Jan_Pollé
MVP Regular Contributor

Hi Kristian,

Please have a look at the sample below.

This works for me:

  • Login to ArcGIS Online
  • Select the relevant layer of your Hosted Feature Layer
  • Create an update_dict with your coded values in the correct order and apply this to the relevant field with update_definition()

Does this answer your question?

Cheers,

Egge-Jan

# This script should be run within a specific ArcGIS/Python environment using the batch file below
# (This batch file comes with the installation of ArcGIS Pro)
# "C:\Program Files\ArcGIS\Pro\bin\Python\scripts\propy.bat" update_coded_value_domains.py

from arcgis.gis import GIS
from provide_credentials import provide_credentials

username, password = provide_credentials()
my_agol = GIS("https://www.arcgis.com", username, password)

# Get the first layer - Layer 0 - of your Hosted Feature Layer with ID <HFL_ID>
lyr = my_agol.content.get('<HFL_ID>').layers[0]

# Add domain values to the field "Color"
update_dict = {"fields": [
    {
      "name": "Color",
      "domain": {"type":"codedValue","name":"MY_CODED_VALUE_DOMAIN",
      "codedValues":[{"name":"Blue","code":"Blue"},
                     {"name":"Green","code":"Green"},
                     {"name":"Orange","code":"Orange"},
                     {"name":"Red","code":"Red"},
                     {"name":"Yellow","code":"Yellow"}]}
    }
  ]}

update = lyr.manager.update_definition(update_dict)
print(update) # Should give {'success': True}

View solution in original post

5 Replies
Egge-Jan_Pollé
MVP Regular Contributor

Hi Kristian,

Please have a look at the sample below.

This works for me:

  • Login to ArcGIS Online
  • Select the relevant layer of your Hosted Feature Layer
  • Create an update_dict with your coded values in the correct order and apply this to the relevant field with update_definition()

Does this answer your question?

Cheers,

Egge-Jan

# This script should be run within a specific ArcGIS/Python environment using the batch file below
# (This batch file comes with the installation of ArcGIS Pro)
# "C:\Program Files\ArcGIS\Pro\bin\Python\scripts\propy.bat" update_coded_value_domains.py

from arcgis.gis import GIS
from provide_credentials import provide_credentials

username, password = provide_credentials()
my_agol = GIS("https://www.arcgis.com", username, password)

# Get the first layer - Layer 0 - of your Hosted Feature Layer with ID <HFL_ID>
lyr = my_agol.content.get('<HFL_ID>').layers[0]

# Add domain values to the field "Color"
update_dict = {"fields": [
    {
      "name": "Color",
      "domain": {"type":"codedValue","name":"MY_CODED_VALUE_DOMAIN",
      "codedValues":[{"name":"Blue","code":"Blue"},
                     {"name":"Green","code":"Green"},
                     {"name":"Orange","code":"Orange"},
                     {"name":"Red","code":"Red"},
                     {"name":"Yellow","code":"Yellow"}]}
    }
  ]}

update = lyr.manager.update_definition(update_dict)
print(update) # Should give {'success': True}
Sara_
by
New Contributor III

Hi 

Thanks for this. I'd like to try this piece of code to reorder my domains on Enterprise. I've over 1,000 domains and when I run the "Sort coded value domain" tool in Pro, my changes are not honored when uploaded to Enterprise.  

Any tips on how to change this code from "my_AGOL" to finding my feature layer on Enterprise? 

Thanks

0 Kudos
CassieGallagher1
New Contributor II

Is there any other way of doing this, without coding?  I am having the same issues, AGOL is sorting by my Coded Value but not Name.  It would be easier for my users to have the Names in order.

CathyWilson2
Occasional Contributor

There is another way. You can change the order in AGOL. Click your feature class that uses the domain. Click the Data tab, then click Fields at the top of the window. Click Edit next to the List of Values (Domain). Move your mouse pointer to the left side of the value until you get the hand pointer. Left-click the mouse button and drag to the desired location. Yes, you have to sort them manually. And the result won't appear when you are linked to the feature class in ArcGIS Pro. And I can't get the list to scroll when I am dragging. (I have a domain that currently has 55 items and had to scroll from the very bottom up to the L's one screen at a time.) But this may be easier than the code method.

CathyWilson2
Occasional Contributor

And then apparently, repeat with each feature class that is using that domain...