1

I am using a Python script (see below) in ArcGIS Pro to update two existing hosted feature layers on ArcGIS Online. The script deletes all records in the existing hosted feature layers and appends the records from feature layers stored in an enterprise geodatabase instance. Everything works fine, and the layers are updated with no issues.

However, after updating the data in the hosted feature services this way the Modified date for the layers in ArcGIS Online does not change. Editing nearly anything about the layers or their details on AGOL does change the Modified date, so I am unsure why deleting all records and appending new ones doesn't count as a modification. These layers are accessed as reference data for field crews using Field Maps, and it would be nice for them to be able to see the last time these layers were updated.

Incidentally, I am updating these layers using a script because the Sharing > Overwrite Web Layer function is disabled when Sync and Download are enabled. Unfortunately, that's not something I can change, as crews need to cache offline copies of map areas that can be used in Field Maps.

Is there a way to change the Modified date of a hosted feature layer directly using ArcPy? If not, is there a way to change elements of the details for the layer such as the Summary or Description that shows up on the item page in AGOL? This would also cause the Modified date to change.

Here is the code I am using:

import arcpy, os, time
from arcgis.gis import GIS
import arcgis.features

# Overwrite Output
arcpy.env.overwriteOutput = True

gis=GIS("pro")
print("\n\nActive Portal in ArcGIS Pro")

startTime = time.time()

featureService = True
hostedTable = False
layerIndex = 0

########################
########################

#Update sites layer
sites = gis.content.search(query='owner:my_owner_name AND title:ArchaeologicalSite', item_type='Feature Service', max_items=1000)
print(sites)

fsItemID = "55d309530b764c289d696eded1ae0a98"

# Get featureService/hostedTable layer
serviceLayer = gis.content.get(fsItemID)
if featureService == True:
    fLyr = serviceLayer.layers[layerIndex]
elif hostedTable == True:
    fLyr = serviceLayer.tables[layerIndex]
    
flc = arcgis.features.FeatureLayerCollection(serviceLayer.url, gis)

# Delete features in batches of 2000. OBJECTIDs will not reset
# Get Min OBJECTID
minOID = fLyr.query(out_statistics=[{"statisticType": "MIN", "onStatisticField": "OBJECTID", "outStatisticFieldName": "MINOID"}])
minOBJECTID = minOID.features[0].attributes['MINOID']

# Get Max OBJECTID
maxOID = fLyr.query(out_statistics=[{"statisticType": "MAX", "onStatisticField": "OBJECTID", "outStatisticFieldName": "MAXOID"}])
maxOBJECTID = maxOID.features[0].attributes['MAXOID']

# If more than 2,000 features, delete in 2000 increments
print("Deleting features")
if (maxOBJECTID - minOBJECTID) > 2000:
    x = minOBJECTID
    y = x + 1999
    while x < maxOBJECTID:
        query = f"OBJECTID >= {x} AND OBJECTID <= {y}"
        fLyr.delete_features(where=query)
        x += 2000
        y += 2000
# Else if less than 2,000 features, delete all
else:
    print("Deleting features")
    fLyr.delete_features(where="1=1")

# Append features from featureService class/hostedTable
print("Appending features")

arcpy.management.Append(r"'layer_with_new_data'", r"hosted_feature_layer", "NO_TEST", [field mapping parameters])


########################
########################

#update surveys layer
surveys = gis.content.search(query='owner:my_owner_name AND title:CulturalResourceSurvey', item_type='Feature Service', max_items=1000)
print(surveys)

fsItemID =  "ef26c44ceb0549acaae598b59fb58ffc"

# Get featureService/hostedTable layer
serviceLayer = gis.content.get(fsItemID)
if featureService == True:
    fLyr = serviceLayer.layers[layerIndex]
elif hostedTable == True:
    fLyr = serviceLayer.tables[layerIndex]
    
flc = arcgis.features.FeatureLayerCollection(serviceLayer.url, gis)

# Delete features in batches of 2000. OBJECTIDs will not reset
# Get Min OBJECTID
minOID = fLyr.query(out_statistics=[{"statisticType": "MIN", "onStatisticField": "OBJECTID", "outStatisticFieldName": "MINOID"}])
minOBJECTID = minOID.features[0].attributes['MINOID']

# Get Max OBJECTID
maxOID = fLyr.query(out_statistics=[{"statisticType": "MAX", "onStatisticField": "OBJECTID", "outStatisticFieldName": "MAXOID"}])
maxOBJECTID = maxOID.features[0].attributes['MAXOID']

# If more than 2,000 features, delete in 2000 increments
print("Deleting features")
if (maxOBJECTID - minOBJECTID) > 2000:
    x = minOBJECTID
    y = x + 1999
    while x < maxOBJECTID:
        query = f"OBJECTID >= {x} AND OBJECTID <= {y}"
        fLyr.delete_features(where=query)
        x += 2000
        y += 2000
# Else if less than 2,000 features, delete all
else:
    print("Deleting features")
    fLyr.delete_features(where="1=1")

# Append features from featureService class/hostedTable
print("Appending features")

arcpy.management.Append(r"'layer_with_new_data'", r"hosted_feature_layer", "NO_TEST", [field mapping parameters])

########################
########################

#print time elapsed
endTime = time.time()
elapsedTime = round((endTime - startTime) / 60, 2)
print("Script finished in {0} minutes".format(elapsedTime))
1
  • To your second point @Vince, that's why I'm asking the question. On AGOL you can obviously see when the records are deleted and then appended, but the fact that the action took place in ArcGIS Pro rather than online apparently does not trigger a change in the modification date. Would changing any of the other fields in the item page (description, summary, etc.) via the script also not lead to a change in the Modified date?
    – lambertj
    Feb 9, 2023 at 17:15

1 Answer 1

1

I had exactly the same problem. (After delete features & appending) I could not find any direct python command to update the date modified on a feature layer. However, I have imported an ESRI python module called OverwriteFS for some of the useful functions it has. One of the commands is called overwriteFeatureService. I was able to run this using the right parameters whereby it does not do an overwrite of the service but it 'Touches' the service - essentially updating the date modified on it. This is the line of code I used to simply perform a 'touch':

outcome = OverwriteFS.overwriteFeatureService( targetitem, updateFile=None, touchItems=True, verbose=None, touchTimeSeries=True, outcome=None, ignoreItems=[], serviceLastModified=0, noIndexes=False, preserveProps=True, noWait=False, noProps=False, converter=None, outPath="", dryRun=False, ignoreAge=False)

Here is a link to download the python module. https://www.arcgis.com/home/item.html?id=d45f80eb53c748e7aa3d938a46b48836

Here is a blog about the tool. It's pretty nifty. https://www.esri.com/arcgis-blog/products/arcgis-living-atlas/data-management/overwrite-feature-services-script-tool-for-automating-updates/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.