Overwriting hosted feature layer Enterprise Portal API for Python

484
2
09-20-2023 07:41 AM
Labels (1)
MatthewSolomon
New Contributor

I have been using a python script for about a month now that does this for me. All of a sudden it is not working. It gets 3/4ths the way through and gives me this error while attempting to overwrite:

Traceback (most recent call last):
File "C:\GIS\scripts\Updating_Web_Layer_Tool_Updated.py", line 138, in <module>
fs = sdItem.publish(overwrite=True)
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py", line 15139, in publish
serviceitem_id = self._check_publish_status(ret, folder)
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py", line 15499, in _check_publish_status
raise Exception("Job failed.")
Exception: Job failed.

 

 

I am not quite sure what it is asking me to do. Any suggestions. Here is the script:

 

import arcpy

import os, sys

from arcgis.gis import GIS

 

 

 

prjPath = r"C:\example\Test.aprx"

 

sd_fs_name = “Feature_Class_Name”

portal = "https://test.com/portal/"

user = "test@user"

password = "password"

 

shrOrg = True

shrEveryone = False

shrGroups = ""

 

relPath = r"C:\example\examples"

sddraft = os.path.join(relPath, "WebUpdate.sddraft")

sd = os.path.join(relPath, "WebUpdate.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)

 

arcpy.StageService_server(in_service_definition_draft=sddraft,

out_service_definition=sd)

 

print("Connecting to {}".format(portal))

gis = GIS(portal, user, password)

 

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 sheEveryone or sheGroups:

    print("Setting sharing options...")

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

 

print("Finished updateing: {} - ID: {}".format(fs.title, fs.id))

0 Kudos
2 Replies
David_McRitchie
Esri Contributor

Hey Matthew,

It might be that there has been a change to permissions on your account, or perhaps an issue with the data.

To help troubleshoot this further can we confirm if manually overwriting the service definition works, or if the script works on other layers?

Many thanks,

David

Esri UK -Technical Support Analyst
0 Kudos
Raul
by
New Contributor III

If you log in to multiple organizations in arcgis pro, you could try adding arcpy.SignInToPortal(portal, user, password) to your script. Make sure you do so before creating the sddraft.

I had a similar issue while trying to overwrite a service a few months back.

0 Kudos