English

How To: Overwrite hosted feature services from ArcGIS Pro to ArcGIS Online using Python

Summary

It is possible to overwrite a hosted feature service using the arcgis.gis.module as a workaround. The module is used to overcome the known limitation of the ArcPy module, with ArcGIS Online credentials in a Python script. The Python script allows the process to be automated. However, Python 3, which is included in an ArcGIS Pro installation, is required.

This article provides instructions to specify the modules and credentials, create the service definition (SD) file, and publish.

Procedure

  1. Import the necessary modules.
import arcpy
import os, sys
from arcgis.gis import GIS
  1. Specify the location of the project file (.aprx).
prjPath = r"C:\Users\SampleFolder\Project_Name.aprx"
  1. Specify the feature service name in ArcGIS Online, including the owner credentials.
sd_fs_name = "HOSTED_FEATURE_SERVICE_NAME"
portal = "http://www.arcgis.com"
user = "USERNAME"
password = "PASSWORD"
  1. Set the desired sharing options. The following code sample sets the service to be shared only with the organization.
shrOrg = True
shrEveryone = False
shrGroups = ""
  1. Specify a local path for storing temporary contents to be used for publishing the service definition draft and service definition file.
relPath = r'C:\Users\SampleFolder_1'
sddraft = os.path.join(relPath, "HOSTED_FEATURE_SERVICE_NAME.sddraft")
sd = os.path.join(relPath, "HOSTED_FEATURE_SERVICE_NAME.sd")
  1. Create a new SDDraft file, and stage the draft to the SD file.
print("Creating SD file")
arcpy.env.overwriteOutput = True
prj = arcpy.mp.ArcGISProject(prjPath)
mp = prj.listMaps()[0]
arcpy.sharing.CreateSharingDraft(mp, sd_fs_name, 'MAP_SERVICE', 'STANDALONE_SERVER')

arcpy.StageService_server(sddraft, sd)
  1. Connect to the specified portal.
print("Connecting to {}".format(portal))
gis = GIS(portal, user, password)
  1. Locate the SD file, update, and overwrite the service on the specified portal.
print("Search for original SD on portal…")
sdItem = gis.content.search("{} AND owner:{}".format(sd_fs_name, user), item_type="Service Definition")[0]
print("Found SD: {}, ID: {} n Uploading and overwriting…".format(sdItem.title, sdItem.id))
sdItem.update(data=sd)
print("Overwriting existing feature service…")
fs = sdItem.publish(overwrite=True)

if shrOrg or shrEveryone or shrGroups:
	print("Setting sharing options…")
	fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)

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

The following shows the full script:

import arcpy
import os, sys
from arcgis.gis import GIS

prjPath = r"C:\Users\SampleFolder\Project_Name.aprx"

sd_fs_name = "HOSTED_FEATURE_SERVICE_NAME"
portal = "http://www.arcgis.com" # Can also reference a local portal
user = "USERNAME"
password = "PASSWORD"

shrOrg = True
shrEveryone = False
shrGroups = ""

relPath = r'C:\Users\SampleFolder_1'
sddraft = os.path.join(relPath, "HOSTED_FEATURE_SERVICE_NAME.sddraft")
sd = os.path.join(relPath, "HOSTED_FEATURE_SERVICE_NAME.sd")


print("Creating SD file")
arcpy.env.overwriteOutput = True
prj = arcpy.mp.ArcGISProject(prjPath)
mp = prj.listMaps()[0]
arcpy.sharing.CreateSharingDraft(mp, sd_fs_name, 'MAP_SERVICE', 'STANDALONE_SERVER')

arcpy.StageService_server(sddraft, sd)

print("Connecting to {}".format(portal))
gis = GIS(portal, user, password)

# Find the SD, update it, publish /w overwrite and set sharing and metadata
print("Search for original SD on portal…")
sdItem = gis.content.search("{} AND owner:{}".format(sd_fs_name, user), item_type="Service Definition")[0]
print("Found SD: {}, ID: {} n Uploading and overwriting…".format(sdItem.title, sdItem.id))
sdItem.update(data=sd)
print("Overwriting existing feature service…")
fs = sdItem.publish(overwrite=True)

if shrOrg or shrEveryone or shrGroups:
	print("Setting sharing options…")
	fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)

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

Related Information

Last Published: 9/15/2020

Article ID: 000023164

Software: ArcGIS Online 8.2 ArcGIS Pro 2.6, 2.5.2, 2.5.1, 2.5, 2.4.3, 2.4.2, 2.4.1, 2.4, 2.3.3, 2.3.2, 2.3.1, 2.3