Overwriting Hosted Feature Layer on ArcGIS Enterprise

12-27-2022 09:33 AM
Labels (1)
New Contributor II


I am trying to automate the updating of hosted feature layers on my organizations Enterprise portal using ArcGIS API for Python. Running through the steps outlined in the documentation does produce a success message and the date modified for the feature layer does update, yet the number of records and geometry stay the same when they should be changing. It is as if an update did not occur. Any advice would be appreciated, below is a sample of the code I am using with paths omitted:

from arcgis.gis import GIS
from arcgis import features

from arcgis.features import FeatureLayerCollection

local_layer_for_overwriting = location of feature class I want to use to update the hosted feature layer (actual location omitted)
portal_layer_itemid = 'itemid for the hosted feature layer on the Enterprise portal (omitted)'
portal_layer_dataitem = gis.content.get(portal_layer_itemid)
flayercol = FeatureLayerCollection.fromitem(portal_layer_dataitem)

0 Kudos
5 Replies
Esri Esteemed Contributor

Hi @DerekRichards,

I recommend using a truncate/append rather than an overwrite.  See the following document.

0 Kudos
New Contributor II


May I ask why truncate/append rather than overwrite? Is there something about overwrite that would cause overwriting not to work correctly?

0 Kudos
Esri Esteemed Contributor

@DerekRichards I personally try to steer clear of overwriting.  If you set any configurations in the Visualization tab for the feature service, these will be overwritten when you overwrite the feature service.  Also, typically you have an ArcGIS Pro project that is used when overwriting a feature service, which contains some settings (i.e. symbology, pop-ups, labels).  A truncate/append will not require you to use the Pro project.

New Contributor II

I think this method could work but it appears I would need an upgrade to the Enterprise as append is only available for Enterprise version of 10.8.1+, and I currently experience append erroring out when I try to use it. Maybe overwrite also needs a 10.8.1+ Enterprise version as well?

0 Kudos
New Contributor III


I overwrite hosted services in portal with a different approach.

I have a project in ArcGIS Pro and have automated python script through task scheduler on the same machine (created generic python function that can be called to update any hosted services in portal by passing required values) which will do the following

  • Get ArcGIS Pro project path
  • Create a sddraft(service file) from that project 
  • Stage that service
  • Connect to Portal and search service to be overwritten
  • Publish new service file (with overwrite option true)

Here is the function, hope that helps

import os
import sys
import logging
import arcpy
import pprint
from datetime import datetime
from arcgis.gis import GIS

def overwriteHostedFeatureService(portal_url, user_name, pwd, prj_path, prj_name, mp_name, sd_fs_name, logfile_name):
logging.basicConfig(filename=logfile_name, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.DEBUG)
logging.info(' ')
logging.info('Service Name - ' + sd_fs_name)
logging.info(' ')

portal = portal_url
user = user_name
password = pwd
fullPrjPath = os.path.join(prj_path, prj_name)
sddraft = os.path.join(prj_path, sd_fs_name + ".sddraft")
sd = os.path.join(prj_path, sd_fs_name + ".sd")

# Create a new SDDraft file, and stage the draft to the SD file.
logging.info("Creating SD file - " + sd)
logging.info("Creating SDDraft file - " + sddraft)
arcpy.env.overwriteOutput = True

prj = arcpy.mp.ArcGISProject(fullPrjPath)
maps = prj.listMaps()
for map in maps:
if map.name == mp_name:
mp = map
logging.info("Map Name - " + mp.name)

arcpy.mp.CreateWebLayerSDDraft(mp, sddraft, sd_fs_name, 'MY_HOSTED_SERVICES', 'FEATURE_ACCESS')

arcpy.StageService_server(sddraft, sd)
logging.info("Staging SD files Done")

# Connect to the specified portal
gis = GIS(portal, user, password)

# Locate the SD file, update, and overwrite the service on the specified portal
sdItem = gis.content.search("{} AND owner:{}".format(sd_fs_name, user), item_type="Service Definition")[0]
#enable this loggin for debuging purpose only
#logging.info("Found SD: {}, ID: {} n Uploading and overwriting…".format(sdItem.title, sdItem.id))
fs = sdItem.publish(overwrite=True)
logging.info("Overwriting existing feature service finish")

# Set the desired sharing options. The following code sets the service to be shared with Everyone and organization.
shrOrg = True
shrEveryone = ""
shrGroups = ""

if shrOrg or shrEveryone or shrGroups:
fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)

logging.info("Finished updating: {} – ID: {}".format(fs.title, fs.id))

0 Kudos