Create domain with python api script

830
1
05-30-2022 01:40 AM
BrianShaw
New Contributor II

My apologies that my question comes with no, I have been asked to transfer data to a feature layer by importing a csv using a python script. I have that working fine but the business has now requested ddl's on the form the csv populates, now so far my attempts were creating the domain on ArcGis online, but when I update the csv data the domain gets deleted, I'm not usually developing ArcGis operability and I'm stumped as to how this really works, any assistence or tutorials will be greatly appreciated.

Tags (3)
1 Reply
Clubdebambos
Occasional Contributor III

Hi @BrianShaw 

Similar process to this post here.

Manually add your domains. We will use this as a template. Once complete run a script similar to below.

from arcgis import GIS

agol = GIS("home")

item = agol.content.get("***fs_id***")

## get the layer in the feature service that you are interested in
## if there is only one then the index is 0
lyr = item.layers[0]

print(lyr.properties.fields)

This will print the JSON of the fields configuration to screen. Open a Notepad and insert the below, copy and paste the JSON in the placeholder.

{
    "fields" : 
        *** copy and paste JSON here *** 
}

Save as a .json file extension. You now have a template that you can edit in the notepad to update. You can add/alter the domains, just stick to the formatting. 

Now that you have the template you can use the below to update the fields as necessary. 

from arcgis import GIS
import json

agol = GIS("home")

item = agol.content.get("***fs_id***")

## open the JSON file
with open(r"C:\path\to\lyr_fields.json") as json_data:
    data = json.load(json_data)

lyr = item.layers[0]

lyr.manager.update_definition(data)

 

~ Mapping my way to retirement