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?
Solved! Go to Solution.
Hi Kristian,
Please have a look at the sample below.
This works for me:
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}
Hi Kristian,
Please have a look at the sample below.
This works for me:
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}
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
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.
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.
And then apparently, repeat with each feature class that is using that domain...