I am trying to save a web layer by overwriting the existing feature service using the FeatureSharingDraft class. I am using ArcGIS Pro 3.4 with Python 3.9. I felt like I followed the documentation pretty closely.
Here’s my script:
import arcpy
import os
portal_url = ""
username = ""
password = ""
arcpy.env.overwriteOutput = True
aprx = arcpy.mp.ArcGISProject("CURRENT")
arcpy.SignInToPortal(portal_url,username,password)
active_portal = arcpy.GetActivePortalURL()
print(f"✅ Successfully signed into: {active_portal}")
gdb_path = r"c:\path.gdb"
feature_layer_name = "test"
sddraft_filename = feature_layer_name + ".sddraft"
sd_filename = feature_layer_name + ".sd"
sddraft_output_filename = os.path.join(gdb_path, sddraft_filename)
sd_output_filename = os.path.join(gdb_path, sd_filename)
print(sd_output_filename)
map_obj = aprx.listMaps()[0]
server_type = "HOSTING_SERVER"
sddraft = map_obj.getWebLayerSharingDraft(server_type, "FEATURE", feature_layer_name)
sddraft.overwriteExistingService = True
print(sddraft)
# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)
# Stage Service
print("Start Staging")
arcpy.server.StageService(sddraft_output_filename, sd_output_filename)
# Share to portal
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, server_type)
print("Finish Publishing")
print(sddraft)
Here is the output:
✅ Successfully signed into: https://portal.maps.arcgis.com/
c:\path.gdb\test.sd
<arcpy.sharing.FeatureSharingDraft object at 0x0000020B90D0D950>
Start Staging
Line 6: arcpy.server.StageService(sddraft_output_filename, sd_output_filename)
File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py, in <lambda>:
Line 512: return lambda *args: val(*gp_fixargs(args, True))
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000814: Invalid file type
ERROR 000814: Invalid file type
Failed to execute (StageService).
I've never used this class before. Why am I using the map object instead of using a layer if I only want to update a single layer?