6

I'm trying to follow the steps in the Esri blog post Updating your hosted feature services with ArcGIS Pro and the ArcGIS API for Python but am running into an error.

To test this I published a simple stand-alone file GDB into an ArcGIS Online web map using ArcGIS Pro's UI tools. This creates a Service Definition, WebMap and associated Feature Layer on ArcGIS Online.

I then made a simple symbology change in ArcGIS Pro and attempted to push the updates to ArcGIS Online using the script. The script runs correctly as far as these lines:

sdItem = gis.content.search(query="title:"+ sd_fs_name + " AND owner: " + user, item_type="Service Definition")[0]
sdItem.update(data=sd)

I can confirm from the timestamps on ArcGIS Online that the Service Definition has been updated, but the script is falling over when trying to update the associated Feature Layer. The script contains this line:

fs = sdItem.publish(overwrite=True)

This gives me the error message

RuntimeError: User cant overwrite this service, using this data, as this data is already referring to another service

I can confirm that the data doesn't actually refer to another service since it's coming from a stand-alone file GDB that isn't used anywhere else. I'm using the same ArcGIS Online administrator's login within ArcGIS Pro and the Python script so I'm definitely the owner of the layer.

How can I overwrite the feature layer using the latest changes in the ArcGIS Pro document?

1
3

I had this problem due to changing the source name in the map doc after publishing the service. Thus, the source name in the service was different from the source name in the map doc. I had to look at the feature's Rest page to determine the original source name used when the feature service was published and changed the map doc to match.

1
  • I changed the name of my layer and got this error. I changed it back to the original name...and I still get the error. Uh oh... Feb 4 '20 at 21:39
0

Recently trying to piece together many different solutions to accomplish this.

Here is one solution using ArcGIS Pro and Python API, 1) publish update and overwrite an existing hosted feature service in ArcGIS Online and 2) the need to manage the capabilities for EDIT and SYNC enabled (needed for offline use) after the update.

Script can be run from command line

"C:\Program Files\ArcGIS\Pro\bin\Python\Scripts\propy.bat" c:\mydata\publish_FS_to_AGO.py

Code:

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

### Start setting variables
# Set the path to the project
prjPath = "c:\mydata\myproproject.aprx"

# Update the following variables to match:
# Feature service/SD name in arcgis.com, user/password of the owner account
sd_fs_name = "My_Hosted_FS"
portal = "https://www.arcgis.com" # Can also reference a local portal
user = "username"
password = "password123"

# Local paths to create temporary content
relPath = os.path.dirname(prjPath)
sddraft = os.path.join(relPath, sd_fs_name + ".sddraft")
sd = os.path.join(relPath, sd_fs_name + ".sd")

# Create a new SDDraft and stage to SD
print("Creating SD file")
arcpy.env.overwriteOutput = True
prj = arcpy.mp.ArcGISProject(prjPath)
mp = prj.listMaps()[0]

# Create Feature Service SharingDraft and set service properties
sharing_draft = mp.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", sd_fs_name)
sharing_draft.summary = ""
sharing_draft.tags = ""
sharing_draft.description = ""
sharing_draft.credits = ""
sharing_draft.useLimitations = ""

# Create Service Definition Draft file
sharing_draft.exportToSDDraft(sddraft)

# Stage Service
try:
    arcpy.StageService_server(sddraft, sd)
    warnings = arcpy.GetMessages(1)
    print(warnings)
except Exception as stage_exception:
    print("Sddraft not staged. Analyzer errors encountered - {}".format(str(stage_exception)))

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

# 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: {} \nUploading and overwriting…".format(sdItem.title, sdItem.id))
sdItem.update(data=sd)

print("Overwriting existing feature service…")
fs = sdItem.publish(overwrite=True)

print("Setting sharing options…")
fs.share(groups="Field Operations")

# Set Feature service capabilites for editing and sync enabled
flc = FeatureLayerCollection.fromitem(fs)

print("Update Edit & Sync capabilites…")
update_dict = {"capabilities": "Create,Delete,Query,Update,Editing,Sync",
               "syncEnabled": True,}

flc.manager.update_definition(update_dict)

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.