Overwrite Hosted Feature Layer Script

1118
2
10-23-2020 12:13 PM
JamesWhite5
New Contributor III

I am attempting to edit a script in order to automate updating a hosted feature layer on our Enterprise page.  I acquired the script from here: 

Automate overwrite web layer, feature class 

Updating your hosted feature services with ArcGIS Pro and the ArcGIS API for Python 

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

When I run the script, it prints "Creating SD file" and then PRO crashes about a minute later.  I have tried running it with PRO closed from IDLE and the get the same result.  I have tried tweaking some of the information with the same result, 6 or 7 times now.

This is the script I am running:

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

### Start setting variables
# Set the path to the project
prjPath = r"C:\Users\Documents\ArcGIS\Projects\VMRC_TEST\VMRC_TEST.aprx"

# Update the following variables to match:
# Feature service/SD name in arcgis.com, user/password of the owner account
sd_fs_name = "VMRC Data"
portal = "https://MyOrganizationsEnterpriseAddress/e4portal/sharing/rest/content/items/e74a684af7ee45a1b346cf9c6ba1d8c1" # Can also reference a local portal
user = ""
password = ""

# Set sharing options
shrOrg = True
shrEveryone = False
shrGroups = ""

### End setting variables

# Local paths to create temporary content
relPath = r"C:\Users\Documents\ArcGIS\Projects"
sddraft = os.path.join(relPath, "WebUpdate.sddraft")
sd = os.path.join(relPath, "WebUpdate.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]
arcpy.mp.CreateWebLayerSDDraft(map_or_layers=mp, out_sddraft=sddraft, service_name=sd_fs_name, server_type="HOSTING_SERVER", service_type="FEATURE_ACCESS", folder_name=None, overwrite_existing_service=True, copy_data_to_server=True, enable_editing=False, allow_exporting=False, enable_sync=True)
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))

One thing I am also not clear about it if I am using PRO while I run the script, and logged onto our enterprise portal, do I need the username and password filled in? We use an API key for ours, so there is no username and password in order to log on our Enterprise site.

For the portal= name, I have it pointing directly to my content page.  Could that be messing it up?

Thank you.

0 Kudos
2 Replies
BrunoGomes_de_Souza
Esri Contributor

Hey James,

Frist you need to change the page. what you need to put is your portal adress, where you do the login.

https://yourportalhere.com.br
Also you need to change the names of Sddraft and sd to the name of your sd in your case VMRC Data. Verify spaces, because it can be a problem.

Important: check and replace all " because it cam be generate a error in your python.

0 Kudos
AmandaBishop2018
New Contributor III

Hey BrunoGomes_de_Souza,

I was wondering if you could take a look at my script and help me?  I am getting the error in red text at the bottom.

This is the script:

# Import arcpy modules
import arcpy
import os, sys
from arcgis.gis import GIS
from arcpy import env
env.workspace= r"C:\Spam\PY\PublicParcels\Project"
env.overwriteOutput = True

### Start setting variables
# location of project file
prjPath = r"C:\Spam\PY\PublicParcels\Project\PublicParcelsUpdate.aprx"

# feature service name in AGOL
sd_fs_name="PublicParcelsTest"

# AGOL credentials
portal="https://acpa.maps.arcgis.com/"
user=""
password=""

# Sharing options
shrOrg=True
shrEverone=True
shrGroups=""

# local path for temp contents
relPath=r"C:\Spam\PY\PublicParcels\Project"
sddraft=os.path.join(relPath,"PublicParcelsTest.sddraft")
sd=os.path.join(relPath,"PublicParcelsTest.sd")

# create new SDDraft file and stage draft to SD file
print("Creating SD file")
arcpy.env.overwriteOutput = True
prj = arcpy.mp.ArcGISProject(prjPath)
mp = prj.listMaps()[0]
arcpy.mp.CreateWebLayerSDDraft(map_or_layers=mp, out_sddraft=sddraft, service_name=sd_fs_name, server_type="HOSTING_SERVER", service_type="FEATURE_ACCESS", folder_name="ACPA Authoritative Data", overwrite_existing_service=True, copy_data_to_server=True, enable_editing=False, allow_exporting=True, enable_sync=False)
arcpy.StageService_server(sddraft, sd)

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

# locate 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 SDE:{}, 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:{}".fomat(fs.title,fs.id))

 

This is the error I am getting:

Traceback (most recent call last):
File "C:\Spam\PY\PublicParcels\OverwritePublicParcel_AGOL.py", line 42, in <module>
arcpy.mp.CreateWebLayerSDDraft(map_or_layers=mp, out_sddraft=sddraft, service_name=sd_fs_name, server_type="HOSTING_SERVER", service_type="FEATURE_ACCESS", folder_name="ACPA Authoritative Data", overwrite_existing_service=True, copy_data_to_server=True, enable_editing=False, allow_exporting=True, enable_sync=False)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\mp.py", line 105, in CreateWebLayerSDDraft
return _convertArcObjectToPythonObject(arcgisscripting._mapping.CreateWebLayerSDDraft(*_gp_fixargs([map_or_layers, out_sddraft, service_name, server_type, service_type, folder_name, overwrite_existing_service, copy_data_to_server, enable_editing, allow_exporting, enable_sync, summary, tags, description, credits, use_limitations], True)))
RuntimeError
>>>

0 Kudos