The Python API has a query_domains method for Feature Layer Collection objects. In order to use this the Feature Service requires a supportsQueryDomains property set to true. I have been unable to add/set this property for a Hosted Feature Service in AGOL. It does exist, however, in a referenced Feature Service in Portal i.e. data stored in SDE. The below code uses the the query_domains method on a Portal item
from arcgis import GIS
from arcgis.features import FeatureLayerCollection
agol = GIS("home")
item = agol.content.get("**fs_id**")
flc = FeatureLayerCollection(item.url, agol)
domains = flc.query_domains(layers=[2])
print(domains)
The output is a list of dictionaries containing properties for each domain. Here is an example of a dictionary in the list.
{
'type': 'codedValue',
'name': 'Domain_Name',
'description': '',
'codedValues': [
{ 'name': 'Description1',
'code': 1
},
{ 'name': 'Description2',
'code': 2
},
{ 'name': 'Description3',
'code': 3
}
],
'fieldType': 'esriFieldTypeSmallInteger',
'mergePolicy': 'esriMPTDefaultValue',
'splitPolicy': 'esriSPTDefaultValue'
}
Attempting to run this code on a Hosted Feature Service on AGOL results in an error.
The requested layer (layerId: queryDomains) was not found.
(Error Code: 400)
We can access the layer properties and extract the domain information for a Hosted Feature Service. The code below will print out a list of dictionaries similar to the query_domains method used in Portal.
from arcgis import GIS
from arcgis.features import FeatureLayerCollection
agol = GIS("home")
item = agol.content.get("**fs_id**")
## list to hold the domain information
domains_list = []
## get domains for specific layer
for fld in item.layers[0].properties.fields:
if fld.domain:
## append each domain into the list
if fld.domain not in domains_list:
domains_list.append(fld.domain)
print(domains_list)