Früher habe ich ein Dokument geschrieben, wie man einen ArcGIS Online Feature Service überschreibt, indem man auf eine Feature-Class verweist und eine Truncate-/Append-Methode verwendet. Ich erhielt viel Feedback zu diesem Dokument, wobei einige Benutzer auf Einschränkungen stießen, wie z. B. dass Anhänge nicht unterstützt werden und das Aktualisieren von Services mit mehreren Layern problematisch ist. Diese Lösung zielt darauf ab, diese Einschränkungen zu beheben. Unten finden Sie ein Skript, um einen ArcGIS Online Feature Service durch Verweis auf ein ArcGIS Pro Projekt zu überschreiben, sowie ein Video zur Verwendung des Skripts. Bitte kommentieren Sie unten, falls es Probleme oder Fragen gibt.<\/P>
<\/P>
import arcpy, os, time, requests, json from arcgis.gis import GIS from arcgis.features import FeatureLayerCollection # Variablen prjPath = r"C:\Projects\GeoNET\GeoNET.aprx" # Pfad zum Pro-Projekt map = 'State Parks' # Name der Karte im Pro-Projekt serviceDefID = '3fa1620c47dc490db43b9370e8cf5df8' # Item-ID der Service-Definition featureServiceID = 'fb42ef7b43154f95b8b6ad7357b7f663' # Item-ID des Feature-Service portal = "https://www.arcgis.com" # AGOL user = "jskinner_rats" # AGOL-Benutzername password = "********" # AGOL-Passwort preserveEditorTracking = True # True/Falsch zum Beibehalten der Editor-Verfolgung aus der Feature-Class unregisterReplicas = True # True/Falsch zum Abmelden vorhandener Replikate # Umgebungsvariablen setzen arcpy.env.overwriteOutput = 1 # Warnungen deaktivieren requests.packages.urllib3.disable_warnings() # Timer starten startTime = time.time() print(f"Verbindung zu AGOL wird hergestellt") gis = GIS(portal, user, password) arcpy.SignInToPortal(portal, user, password) # Lokale Pfade zum Erstellen temporärer Inhalte sddraft = os.path.join(arcpy.env.scratchFolder, "WebUpdate.sddraft") sd = os.path.join(arcpy.env.scratchFolder, "WebUpdate.sd") sdItem = gis.content.get(serviceDefID) # Neues SDDraft erstellen und in SD umwandeln print("SD-Datei wird erstellt") arcpy.env.overwriteOutput = True prj = arcpy.mp.ArcGISProject(prjPath) mp = prj.listMaps(map)[0] serviceDefName = sdItem.title arcpy.mp.CreateWebLayerSDDraft(mp, sddraft, serviceDefName, 'MY_HOSTED_SERVICES', 'FEATURE_ACCESS', '', True, True) arcpy.StageService_server(sddraft, sd) # Vorhandenen Feature-Service referenzieren, um Eigenschaften zu erhalten fsItem = gis.content.get(featureServiceID) flyrCollection = FeatureLayerCollection.fromitem(fsItem) properties = fsItem.get_data() capabilities = flyrCollection.manager.properties # Thumbnail und Metadaten abrufen thumbnail_file = fsItem.download_thumbnail(arcpy.env.scratchFolder) metadata_file = fsItem.download_metadata(arcpy.env.scratchFolder) # Vorhandene Replikate abmelden enableSync = False if unregisterReplicas: if flyrCollection.properties.syncEnabled: enableSync = True print("Vorhandene Replikate werden abgemeldet") for replica in flyrCollection.replicas.get_list(): replicaID = replica['replicaID'] flyrCollection.replicas.unregister(replicaID) # Feature-Service überschreiben sdItem.update(data=sd) print("Vorhandenen Feature-Service überschreiben") if preserveEditorTracking: pub_params = {"editorTrackingInfo" : {"preserveEditUsersAndTimestamps":'true'}} fs = sdItem.publish(overwrite=True, publish_parameters=pub_params) else: fs = sdItem.publish(overwrite=True) # Service mit vorherigen Eigenschaften aktualisieren print("Service-Eigenschaften werden aktualisiert") item_properties = {"text": json.dumps(properties)} fs.update(item_properties=item_properties) flyrCollection.manager.update_definition(capabilities) # Thumbnail und Metadaten aktualisieren print("Thumbnail und Metadaten werden aktualisiert") fs.update(thumbnail=thumbnail_file, metadata=metadata_file) print("Temporäres Verzeichnis wird geleert") arcpy.env.workspace = arcpy.env.scratchFolder for file in arcpy.ListFiles(): if file.split(".")[-1] in ('sd', 'sddraft', 'png', 'xml'): arcpy.Delete_management(file) endTime = time.time() elapsedTime = round((endTime - startTime) / 60, 2) print(f"Skript in {elapsedTime} Minuten abgeschlossen")
Update 2/2/24: Option hinzugefügt, vorhandene Replikate abzumelden
Update 12/2/24: Sync wird wieder aktiviert, wenn er zuvor aktiviert war
@ModernGISKannst du den Service, den du versuchst zu aktualisieren, mit einer Gruppe teilen und mein Konto einladen (jskinner_rats)?
@jskinner
Ich beginne, einige Probleme beim Aktualisieren von AGOL aus einer File GDB, die von ArcGIS Pro ausgeführt wird, zu haben. Ich habe Ihr Skript kürzlich erneut heruntergeladen und erhalte immer noch dasselbe Problem. Die File GDB (Feature-Classes, Tabellen und Relationship-Classes) wird in einen einzelnen Hosted Feature Layer auf AGOL übertragen (Bearbeitung aktiviert und Synchronisierung aktiviert).
Ich erhalte diesen Fehler, egal was ich mache. Ich schaue, ob ich etwas übersehe, das ich beheben kann?
Ich verwende diesen Hosted Feature Layer für Field Maps mit Tasks und benötige ihn synchronisiert für Offline-Funktionen.
Ich schätze Ihre Unterstützung.
One more post here to close the loop - The bug for this is indeed BUG-000115592 and it's closed as a Known Limitation. The enhancement request is ENH-000179977 "Allow overwriting a hosted feature layer in ArcGIS Online when a joined view is present or a view with an area of interest is defined". It's In Review. And I'm hoping the documentation will be corrected to provide clarification on the known limitation. But most of all I hope with some Kudos on that Idea, Esri can implement this ability. Thanks.
@JakeSkinner You're welcome. It's not documented well anywhere. I'm waiting on more info from Tech support. I also found BUG-000115592 and Idea: https://community.esri.com/t5/arcgis-online-ideas/allow-overwriting-a-hosted-feature-layer-in-arcgis/idi-p/1672183 Give it a Kudo please, everyone!
Thanks @AndreaB_ for reporting this back on the thread. I was not aware of this limitation.
Hi @JakeSkinner ,
Thank you for testing it out. I just did some testing also with Pro 3.6.1. The issue is: the feature layer has a View Layer created with an Area of Interest defined within it.
When I create my View Layer in AGO:
This blocks overwriting of the feature layer. 🛑
Esri tech support has provided some links:
--
I am VERY surprised this is expected behavior. I would expect this to be a bug and functionality that needs to be fixed as soon as possible.
@AndreaB_ I could not reproduce this using Pro 3.6.1. Could you publish another service, create a view, and test the script with the new service? This will determine if it's an issue with the script/API, or with the service.
It looks like I spoke too soon about the script working. Even though the date shows updated in AGO my data isn't updated. I do have a View Layer created on this hosted feature layer. Do you think that's the problem? Here is what I get if I run the .py code in IDLE:
Python 3.11.11 (main, Mar 3 2025, 15:29:37) [MSC v.1938 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
= RESTART: E:\Code\Overwrite Feature Service\Overwrite Feature Service.py
Connecting to AGOL
Creating SD file
Overwriting existing feature service
Traceback (most recent call last):
File "E:\Code\Overwrite Feature Service\Overwrite Feature Service.py", line 68, in <module>
fs = sdItem.publish(overwrite=True, publish_parameters=pub_params)
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\arcgis\gis\__init__.py", line 16825, in publish
return self._publish(**params)
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\arcgis\gis\__init__.py", line 17201, in _publish
serviceitem_id = self._check_publish_status(ret, folder)
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\arcgis\gis\__init__.py", line 17563, in _check_publish_status
raise Exception("Job failed.")
Exception: Job failed.
The .py seems to run in Windows task scheduler because the item detail page in AGO says "item updated" with today's date. But the Last Run result in Windows Task Scheduler says 0x1.
Do you have any ideas?
Thanks,
A
Sorry for the confusion - I edited my post from last week. Your script is working great. I'm using ArcGIS Pro 3.5.5.
The bug, BUG-000171689, comes from ArcGIS Online and Esri tech support tells me the defect is currently under review with the development team.
But I'm using your script as a workaround to avoid that bug. Without your script I would be "cooked", as the kids say. 😊
Thank you!!
Hi @AndreaB_, I could not reproduce this. What version of ArcGIS Pro are you running? I tested with ArcGIS Pro 3.6.1, which is version 2.4.2 of the arcgis module.
Hi @JakeSkinner,
I am running into this AGO bug, BUG-000171689, https://support.esri.com/en-us/bug/after-truncating-the-feature-layer-the-visibility-scale-bug-000171689 "After truncating the feature layer, the visibility scale range of feature layer has been unexpectedly reset at the REST endpoint" when I run an ArcGIS Pro model that truncates and appends my hosted feature layer in AGO. So I thought I could use your script as a work around. Your script is great - but it still resets the visibility scale range for the feature layers in AGO.
If I do a manual Overwrite Feature Layer using ArcGIS Pro - it keeps the visibility scale range for my layers. But I need to run this as a script nightly.
Are you able to add this to your code to keep the visibility range on the layers?
I'm not sure how else to work around this at this point.
***Edit - And.....nevermind. Your script works as a perfect work around and it does maintain the visibility range. I had unknowingly run the Model after your script so I had bad results.
Thank you!
@JakeSkinner
Since I have last integrated this app into my update workflows, I have done a tremendous amount of development on our AGOL platform. The main dataset is maintained and edited on ArcGIS Pro from our internal server and updated to AGOL for usage in Field Maps, Survey123 and Workforce.
I have started including attachments to feature classes and related GDB tables with relationship classes.
One of the issues I have recently found is when I run the script and it updates a Hosted Feature Layer on AGOL (Feature classes, tables, relationships, etc), the data appears to be updated when I open the data dialog in My Content. However, when I open the map in Map View/Field Maps, the related tables are not reflecting the updates from the same Hosted Feature Layer. This is all from the python script.
When I run the tool in ArcGIS Pro to overwrite the hosted feature layer, the related tables in the Map Viewer do infact reflect the updated changes.
Any thoughts on the script you provided or something I am overlooking?
@JakeSkinner So reading through the script, no matter what you still need to disable sync to update the feature service? Has anyone tested to see if their Offline Map Areas are preserved after this script is run?
This is fantastic! 🙏
One suggestion to the recording, which by the way, did an excellent job of listing possible changes user like me would potentially make, is having a couple view layer created. I would assume using the script wouldn't disconnect view layer or receive any failed update error, but it would be comforting to see in the video.
Wow!!
I learned lot of things from this. Thank you.
@JakeSkinner thats sorted it. Thanks.Although we had deployed the Local Intranet change to all users, it had not been deployed on the server running the Overwrite (Portal) Feature Service script. Thanks for your help. Much appreciated.
@TomShewring this should not occur if you're running command prompt as a named user in Portal, and you have Portal's URL added to the Local Intranet (i.e. Start > Internet Options > Security tab > Local Intranet > Sites > Advanced > specify the portal URL (minus the web adaptor) > Add):
Hi @JakeSkinner , changing to these settings (and using the Portal WebAdaptor address) -when I run the script from the command line I get a security challenge
So I could not run this as a scheduled task on the Windows server
@TomShewring try removing the username/password variables from the following lines:
These should not be needed if you Windows Authentication is enabled. It will use the window's account that's signed into the server.
@JakeSkinner 1. Yes Windows Authentication is enabled on Portal WebAdaptor2. Enterprise (Portal / Federated Server / Datastore) version 11.33. I have tried the script with(i) ArcGISPro 3.3.1 -
and(ii) ArcGISPro 3.1.4
@TomShewring
@JakeSkinner , thanks - moving the 'Clear scratch directory' section to the beginning of the script, above the section that connects to AGOL/Portal - this works.One further thing I don't understand is - I have to use the <portalurl>:7443/arcgis address. If I use the WebAdaptor address I get errors such as these (even though I am using the same username and password in each scenario) -Traceback (most recent call last):File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\auth\_auth\_winauth.py", line 75, in __init__creds = gssapi.raw.acquire_cred_with_password(File "gssapi\raw\ext_password.pyx", line 75, in gssapi.raw.ext_password.acquire_cred_with_passwordgssapi.raw.exceptions.BadNameError: Major (131072): An invalid name was supplied, Minor (2529639136): Configuration file does not specify default realmFile "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\auth\_auth\_winauth.py", line 84, in __init__raise Exception("Please ensure gssapi is installed")Exception: Please ensure gssapi is installed
Do you know why this is?
@TomShewring I've seen this error occur sporadically in some implementations (mine included), and I'm unable to find a cause. To workaround the issue, I recommend moving the Clear scratch directory section to the beginning of the script. For example, move this section of code above the section that connects to AGOL/Portal.
Hi @JakeSkinner , a hosted feature service.I have got the 'Update service with previous properties' working - I had to use the siteadmin (default Administrator) account when connecting to Enterprise (Portal).I now have a error in the 'Clearing scratch directory' section. I do not have ArcGISPro open (I am running this script from the command line not from within ArcGISPro) - so I do not know what application could be locking the WebUpdate.sd?Traceback (most recent call last):File "E:\AGOL_Scripts\Geoprocessing\ScheduledTools\NHLEonPortal\updateNHLEonPortal2.py", line 102, in <module>arcpy.Delete_management(file)File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 7665, in Deleteraise eFile "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 7662, in Deleteretval = convertArcObjectToPythonObject(gp.Delete_management(*gp_fixargs((in_data, data_type), True)))^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>return lambda *args: val(*gp_fixargs(args, True))^^^^^^^^^^^^^^^^^^^^^^^^^^^^arcgisscripting.ExecuteError: ERROR 000601: Cannot delete C:\Users\tshewring\AppData\Local\Temp\scratch\WebUpdate.sd. May be locked by another application.Failed to execute (Delete).
@TomShewring are you attempting to update a hosted feature service, or a referenced service?
I am running this script with an internal Enterprise (Portal) with IWA.
The script is working for me up until 'Update service with previous properties'
Traceback (most recent call last):File "<string>", line 81, in <module>File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\json\__init__.py", line 346, in loadsreturn _default_decoder.decode(s)^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\json\decoder.py", line 337, in decodeobj, end = self.raw_decode(s, idx=_w(s, 0).end())^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\json\decoder.py", line 355, in raw_decoderaise JSONDecodeError("Expecting value", s, err.value) from Nonejson.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
@cog_GIS_Admin here is helpful document on how use Windows Task Scheduler with python scripts:
https://community.esri.com/t5/python-documents/schedule-a-python-script-using-windows-task/ta-p/915861
Thanks Jake, that was the issue. Now working on getting it to auto run with the server task scheduler.
@cog_GIS_Admin check the casing of your username, this is case sensitive. For example, if your AGOL login is cog_GIS_Admin, and you specify cog_gis_admin, it will not authenticate.....even though cog_gis_admin will work in a web browser.
My post keeps getting marked as spam, not sure why. I'll try again. This script is just what I was looking for, but I am getting an error trying to log into AGOL.
Connecting to AGOLTraceback (most recent call last):File "E:\Scripts\Workorders_CD\NewOverwriteWebLayers.py", line 30, in <module>arcpy.SignInToPortal(portal, user, password)File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\__init__.py", line 2609, in SignInToPortalreturn _SignInToPortal(*args, **kwargs)ValueError: Error signing on to https://arcgis.com/.Message : sDetails : Unable to generate token.
Any ideas?
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registrieren Sie ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.