Programmatically attribute fields for hosted data in portal or arconline

844
2
Jump to solution
06-20-2022 10:10 AM
JesSkillman
New Contributor III

Hello,

I'm wondering if there is a way to use ArcGIS API for python (or something else) to programmatically edit the field description and field value types for a hosted layer on ArcOnline or Portal. A description for how to do this via the layer's item page > Data tab is here:

 https://doc.arcgis.com/en/arcgis-online/manage-data/describe-fields.htm#ESRI_SECTION1_C30D73392D964D...

But I'd like to be able to automate this because some of my layers have quite a lot of attributes. If, as it happens, layers break for some reason and I have to re-publish, I don't want to manually add this information in again.

Here is an example with a simple desciption and field value type manually added in. 

JesSkillman_0-1655744861348.png

 

 

0 Kudos
1 Solution

Accepted Solutions
Clubdebambos
Occasional Contributor III

Hi @JesSkillman yes this is possible. I hope the workflow below helps.

First of all, do it manually for each field. We will use this as a template. Once complete run a similar script as 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 to screen. In a notepad enter the following and then copy and paste the printed JSON where marked below.

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

Save the file with .json extension. You now have a template for the fields in that specific layer of the feature service. You can edit the template if you liked, alter alias names for example, update domain vales and names. Some things cannot be altered like the field name and type. 

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)

I successfully deploy this across projects where similar data is used. One JSON file to help maintain a similar layer across multiple feature services.

 

~ learn.finaldraftmapping.com

View solution in original post

2 Replies
Clubdebambos
Occasional Contributor III

Hi @JesSkillman yes this is possible. I hope the workflow below helps.

First of all, do it manually for each field. We will use this as a template. Once complete run a similar script as 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 to screen. In a notepad enter the following and then copy and paste the printed JSON where marked below.

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

Save the file with .json extension. You now have a template for the fields in that specific layer of the feature service. You can edit the template if you liked, alter alias names for example, update domain vales and names. Some things cannot be altered like the field name and type. 

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)

I successfully deploy this across projects where similar data is used. One JSON file to help maintain a similar layer across multiple feature services.

 

~ learn.finaldraftmapping.com
JesSkillman
New Contributor III

Awesome! Thank you so much! I tried looking at the field properties before I added the descriptions and couldn't figure it out. This is much easier.

0 Kudos