Select to view content in your preferred language

Append reports success, but no rows appended to hosted feature service.

113
1
Wednesday
EricEagle
Frequent Contributor

I'm attempting to test some access controls on an ArcGIS Enterprise instance by running a simple script that appends rows from a feature class into a hosted feature service.

In order to ensure schema match, I took one feature class, and published it as a hosted feature service.  Then I selected 4 or 5 rows from that same feature class, exported them to a new feature class, changed some things around in the attributes, saved, and then attempted to append those rows.  I am not receiving any error from the following code, but none of the rows are updating the hosted feature service.

from arcgis import GIS
import arcpy
import os

url = 'url to my ArcGIS Enterprise instance'
username = 'my username'
pw = 'my pw'
gis = GIS(
    url=url,
    username=username,
    password=pw,
    verify_cert=False)

target_layer = gis.content.get('layer id').layers[0]
print(target_layer) #succeeds

#Data I need to append to the feature service layer
fgdb = 'path/to/fgdb.gdb'
append_layer = os.path.join(fgdb, 'append_features')

target_layer.append(append_layer)
print('Local layer appended to service successfully.')

Again, no errors, but nothing committed.

I read into the docs and it seems like I may not be able to append a feature class to the feature service directly - that is fine, I just ran a simple conversion to GeoJSON, editing lines 18-22 like so:

fgdb = 'path/to/fgdb.gdb'
append_layer = os.path.join(fgdb, 'append_features')
outfile = 'path/to/outfile.geojson'
append_feats = arcpy.conversion.FeaturesToJSON(append_layer, outfile, geoJSON='GEOJSON')

target_layer.append(append_feats)

This also succeeded in creating the .geojson file, and returned no errors, but again, no rows were actually committed to the target service layer.

What am I doing wrong, and how do I ensure a meaningful error is reported when a hosted feature layer's .append() action fails?

Tags (3)
0 Kudos
1 Reply
Clubdebambos
MVP Regular Contributor

Hi @EricEagle,

A couple of things here. The first is to check the documentation for append()

The way I always think about it is that the append doesn't take data from disk, it takes data from an ArcGIS Online/Portal Item. Your gdb needs to be hosted in Portal/AGOL (or your GeoJSON). 

If you simply print(target_layer.append(append_layer)) you will get True or False returned. Many of the ArcGIS API for Python methods fail gracefully in that it will give you more details about what us unsuccessful/successful but won't error and exit the script.

Your script should look something more like the below...

target_layer.append(
    item_id ="THE_ID_OF_THE_HOSTED_ZIPPED_GDB",
    upload_format = "filegdb",
    source_table_name = "THE_NAME_OF_FC_IN_THE_GDB",
    field_mappings = DICTIONARY - SEE DOCS IF NEEDED
)

The append can be very finicky and detrimental to your mental health so take deep breaths. You also have other options. You can use ArcPy and the Append() geoprocessing tool.

arcpy.management.Append(
    inputs = "PATH_TO_FEATURE_CLASS",
    target = "FEATURE_LAYER_URL",
    schema_type = "NO_TEST", (will only match up if field names are teh same - see docs)
    feature_service_mode = "USE_FEATURE_SERVICE_MODE" (optimisation available with ArcGIS Pro 3.5+)
)

 Let us know if any of this helps or if you need any further guidance just let me know.

All the best,

Glen

~ learn.finaldraftmapping.com