Hi @ClintBoaz1,
Does either of the below suit your workflows?
@DonMorrison1 is correctwhere/how the original is published can affect overwriting.
ArcPy
You can publish from ArcGIS Pro manually or programmatically. Check out my blog post here for publishing with ArcPy. When overwriting you just need to add in the code on line 77 below. Even if you initially publish manually from Proyou can still use Python (ArcPy) to overwrite.
import arcpy
import os
################################################################################
## Esri Documentation:
## https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/arcgisproject-class.htm
## https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/map-class.htm
## https://pro.arcgis.com/en/pro-app/latest/arcpy/sharing/featuresharingdraft-class.htm
## https://pro.arcgis.com/en/pro-app/latest/tool-reference/server/stage-service.htm
## https://pro.arcgis.com/en/pro-app/latest/tool-reference/server/upload-service-definition.htm
##
##
## ArcGIS Pro Version 3.4.0
##
################################################################################
################################################################################
## INPUT REQUIRED #############################################################
## the path to the APRX that contains the Map with the layers to publish
aprx_path = r"C:\path\to\project\your_project.aprx"
## the name of the Map in the APRX that contains the layers to publish
map_name = "NAME OF MAP"
## a folder to stage the definition files
staging_fldr = r"C:\path\to\staging_folder"
## some tags for our Hosted Feature Service
tags = "tag1,tag2,tag3"
## a description for our Hosted Feature Service
description = "This is our description"
## a summary for our Hosted Feature Service
summary = "This is our summary"
################################################################################
## ACCESS ARCGIS PRO COMPONENTS ###############################################
## access the APRX
aprx = arcpy.mp.ArcGISProject(aprx_path)
## access the correct map
m = aprx.listMaps(map_name)[0]
################################################################################
## STAGING #####################################################################
## the service definition filenames and output paths
sddraft_output_filepath = os.path.join(staging_fldrf"{map_name}.sddraft")
sd_output_filepath = os.path.join(staging_fldrf"{map_name}.sd")
## if either of the output files already existsthen delete
if arcpy.Exists(sddraft_output_filepath):
arcpy.Delete_management(sddraft_output_filepath)
if arcpy.Exists(sd_output_filepath):
arcpy.Delete_management(sd_output_filepath)
################################################################################
## MANIPULATE THE SD DRAFT ######################################################
## use the Map object getWebLayerSharingDraft method
## returns a FeatureSharingDraft object
sd_draft = m.getWebLayerSharingDraft(
server_type = "HOSTING_SERVER",
service_type = "FEATURE",
service_name = map_name
)
## alter some properties of the sd_draft
sd_draft.description = description
sd_draft.summary = summary
sd_draft.tags = tags
sd_draft.allowExporting = True
sd_draft.overwriteExistingService = True # OVERWRITE
## create the Service Definition Draft file
sd_draft.exportToSDDraft(sddraft_output_filepath)
################################################################################
## STAGE THE SERVICE ###########################################################
## stage the service
arcpy.server.StageService(
in_service_definition_draft = sddraft_output_filepath,
out_service_definition = sd_output_filepath
)
################################################################################
## PUBLISH TO ARGIS ONLINE #####################################################
## publish to agol
arcpy.server.UploadServiceDefinition(
in_sd_file = sd_output_filepath,
in_server = "HOSTING_SERVER"
)
################################################################################
print("\nSCRIPT COMPLETE")
There are more advanced workflows for publishing and overwriting that digs into the xml for various settingyou can find these in the documentation links at the top of the script. The above is really a minimum required to publish/overwrite.
ArcGIS API for Python
Publish an original Feature Service based on the File Geodatabase.
from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
## Path to File Geodatabase Zipped
gdb = r"C:\path\to\zipped_gdb.zip"
## Properties
gdb_properties = {"title" : "PUBLISHED_FROM_API""type" : "File Geodatabase"}
## Access ArcGIS Online
agol = GIS("home")
## Add File Geodatabase to AGOL
gdb_item = agol.content.add(item_properties=gdb_propertiesdata=gdb)
## Publish as new feature service
new_fs = gdb_item.publish()
print(new_fs.id)
## you can delete the File Geodatabase added to AGOL
Overwrite...
from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
## Path to File Geodatabase Zipped with data to use to overwrite
gdb = r"C:\path\to\zipped_gdb.zip"
## Access ArcGIS Online
agol = GIS("home")
## Fature Service as an Item object - the one to overwrite
item = agol.content.get("ITEM_ID")
## Overwrite
flc = FeatureLayerCollection.fromitem(item)
print(flc.manager.overwrite(gdb))
Note! The name of the file geodatabase and the feature class(es) must be the same as the original that was published or else you will get
ValueError: The name and extension of the file must be the same as the original data.
I hope that helps.
All the best,
Glen
~ learn.finaldraftmapping.com