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.

Note:
This procedure only works on hosted feature services with existing service definition files.

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, "temporary service name.sddraft")
sd = os.path.join(relPath, "temporary service name.sd")
  1. Create a new SDDraft file, and stage the draft to the SD file.
Note:
In sharing_draft = mp.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", sd_fs_name) section, keep parameter "HOSTING_SERVER" and "FEATURE" as the default parameter. Only edit the defined parameter, sd_fs_name, with the same variable name defined by the user.
print("Creating SD file")
arcpy.env.overwriteOutput = True
prj = arcpy.mp.ArcGISProject(prjPath)
mp = prj.listMaps()[0]

#define a new object to utilize the getWebLayerSharingDraft function with HOSTING_SERVER and FEATURE as the default parameters
sharing_draft = mp.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", sd_fs_name)
sharing_draft.summary = "My Summary"
sharing_draft.tags = "My Tags"
sharing_draft.description = "My Description"
sharing_draft.credits = "My Credits"
sharing_draft.useLimitations = "My Use Limitations"

sharing_draft.exportToSDDraft(sddraft)
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 specified in step 3, update, and overwrite the service on the specified portal.
print("Search for original SD on portal…")
print(f"Query: {sd_fs_name}")
sdItem = gis.content.search(query=sd_fs_name, item_type="Service Definition")
i = 0
while sdItem[i].title != sd_fs_name:
            i += 1
print('"Item Found")
print(f'item[i].title = {sdItem[i].title}, sd_fs_name = {sd_fs_name}')
item = sdItem[i] 
item.update(data=sd)

print("Overwriting existing feature service…")
fs = item.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'
sddraft = os.path.join(relPath, "temporary service name.sddraft")
sd = os.path.join(relPath, "temporary service name.sd")

print("Creating SD file")
arcpy.env.overwriteOutput = True
prj = arcpy.mp.ArcGISProject(prjPath)
mp = prj.listMaps()[0]

sharing_draft = mp.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", sd_fs_name)
sharing_draft.summary = "My Summary"
sharing_draft.tags = "My Tags"
sharing_draft.description = "My Description"
sharing_draft.credits = "My Credits"
sharing_draft.useLimitations = "My Use Limitations"

sharing_draft.exportToSDDraft(sddraft)
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…")
print(f"Query: {sd_fs_name}")
sdItem = gis.content.search(query=sd_fs_name, item_type="Service Definition")
i=0
while sdItem[i].title != sd_fs_name:
            i += 1
print('Item Found')
print(f'item[i].title = {sdItem[i].title}, sd_fs_name = {sd_fs_name}')
item = sdItem[i] 
item.update(data=sd)

print("Overwriting existing feature service…")
fs = item.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: 11/10/2021

Article ID: 000023164

Software: ArcGIS Pro 2.4.3, 2.4.2, 2.4.1, 2.4, 2.3.3, 2.3.2, 2.3.1, 2.3, 2.2.4, 2.2.3, 2.2.2, 2.2.1, 2.2, 2.1.3, 2.1.2, 2.1.1, 2.1, 2.0.1, 2.0