Select to view content in your preferred language

Add and edit fields in ArcGIS Online hosted Feature Layer table using Python API

1791
2
Jump to solution
02-06-2022 05:42 AM
Manu
by
Occasional Contributor

I have a FeatureLayer on ArcGIS Online. It contains a Layer and a Table. Now, I want to add columns to that table (in case they were not already added) and update the values for the cells in those columns based on another column. I tried this from an ArcGIS Pro Python Notebook:

gis = GIS("pro")

ArcGIS_content = "my_feature_layer_ID"

## get ArcGIS Online layer
AGOL_survey_layer = gis.content.get(ArcGIS_content)

# get table
species_table = AGOL_survey_layer.tables[0]

species_table_df = species_table.query(as_df = True)
table_fields = list(species_table_df.columns.values)

# add new fields, if not already present
new_fields = ["field0", "field1", "field2"]

for field in new_fields:
    if field not in table_fields:
        arcpy.management.AddField(species_table, field_name = field,
            field_type = "TEXT")

# update field values
search_cursor = arcpy.da.SearchCursor(species_table, ["input_field"])
update_cursor = arcpy.da.UpdateCursor(species_table, new_fields)

for row_search, row_update in zip(search_cursor, update_cursor):
    value = row_search
    row_update.updateRow(["valueField0", "vaueField1", "valueField2"])

However, this won't work. E.g., it outputs

RuntimeError: 'in_table' is not a table or a featureclass

 

How can I solve the issues here? The examples on pro.arcgis.com show only examples for offline tables. However, I do not want to download anything. It should update the hosted FeatureLayer's table directly on ArcGIS Online.

0 Kudos
1 Solution

Accepted Solutions
Clubdebambos
Occasional Contributor III

Hi @Manu 

You need to use the url of the table/layer when interacting AGOL items with arcpy.

species_table = AGOL_survey_layer.tables[0].url

 

~ learn.finaldraftmapping.com

View solution in original post

2 Replies
Clubdebambos
Occasional Contributor III

Hi @Manu 

You need to use the url of the table/layer when interacting AGOL items with arcpy.

species_table = AGOL_survey_layer.tables[0].url

 

~ learn.finaldraftmapping.com
MapOfTheSolutionatique
New Contributor II

Hello,

I realize this is an old topic, but I stumbled upon this from Google and wanted to clarify that for me, <layer_name>.layers[0].url worked instead.

Thank you for contributing your answer!