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?
Solved! Go to Solution.
Do not place these in the file geodatabase, set them to go into another folder.
sddraft_output_filename = os.path.join(gdb_path, sddraft_filename)
sd_output_filename = os.path.join(gdb_path, sd_filename)
Your feature_feature_layer must be the name of the feature service you are attempting to overwrite. All Feature Service names in ArcGIS Online are unique, not to be mistaken with title, which can be duplicated. You would find the name in the service url: https://services-eu1.arcgis.com/****/arcgis/rest/services/PUBLISHED_FROM_API/FeatureServer for example
import arcpy
import os
portal_url = ""
username = ""
password = ""
staging_folder = "path\\to\\staging\\folder"
server_type = "HOSTING_SERVER"
## access the APRX
aprx = arcpy.mp.ArcGISProject("CURRENT")
## you do not need this if already signed in to AGOL in ArcGIS Pro
arcpy.SignInToPortal(portal_url,username,password)
## you do not need this if already signed in to AGOL in ArcGIS Pro
active_portal = arcpy.GetActivePortalURL()
print(f" Successfully signed into: {active_portal}")
## this will be the name of the feature service you are overwriting!
feature_layer_name = "test"
## filenames
sddraft_filename = feature_layer_name + ".sddraft"
sd_filename = feature_layer_name + ".sd"
## filepaths
sddraft_output_filename = os.path.join(staging_folder, sddraft_filename) ## staging folder
sd_output_filename = os.path.join("staging_folder, sd_filename) ## staging folder
print(sd_output_filename)
## get the Map object
map_obj = aprx.listMaps()[0] ## get the map of interest
sddraft = map_obj.getWebLayerSharingDraft(server_type, "FEATURE", feature_layer_name) # feature_layer_name is the name of the Feature Service you are overwriting
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)
I hope that helps.
All the best,
Glen
@Clubdebambos I have read your blog post and seen a couple posts that you have provided insight. Curious if you have any thoughts on this?
Do not place these in the file geodatabase, set them to go into another folder.
sddraft_output_filename = os.path.join(gdb_path, sddraft_filename)
sd_output_filename = os.path.join(gdb_path, sd_filename)
Your feature_feature_layer must be the name of the feature service you are attempting to overwrite. All Feature Service names in ArcGIS Online are unique, not to be mistaken with title, which can be duplicated. You would find the name in the service url: https://services-eu1.arcgis.com/****/arcgis/rest/services/PUBLISHED_FROM_API/FeatureServer for example
import arcpy
import os
portal_url = ""
username = ""
password = ""
staging_folder = "path\\to\\staging\\folder"
server_type = "HOSTING_SERVER"
## access the APRX
aprx = arcpy.mp.ArcGISProject("CURRENT")
## you do not need this if already signed in to AGOL in ArcGIS Pro
arcpy.SignInToPortal(portal_url,username,password)
## you do not need this if already signed in to AGOL in ArcGIS Pro
active_portal = arcpy.GetActivePortalURL()
print(f" Successfully signed into: {active_portal}")
## this will be the name of the feature service you are overwriting!
feature_layer_name = "test"
## filenames
sddraft_filename = feature_layer_name + ".sddraft"
sd_filename = feature_layer_name + ".sd"
## filepaths
sddraft_output_filename = os.path.join(staging_folder, sddraft_filename) ## staging folder
sd_output_filename = os.path.join("staging_folder, sd_filename) ## staging folder
print(sd_output_filename)
## get the Map object
map_obj = aprx.listMaps()[0] ## get the map of interest
sddraft = map_obj.getWebLayerSharingDraft(server_type, "FEATURE", feature_layer_name) # feature_layer_name is the name of the Feature Service you are overwriting
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)
I hope that helps.
All the best,
Glen
Thanks Glen! That worked.
@Clubdebambos One last question because that didn't actually completely work, when I update run the code it updates my layer to include information that is also in the map. It included a table that was also in my contents. How do I just update the target layer with the changes that I have made to that target layer? Why do I use the map object and not the layer itself when using getWebLayerSharingDraft() like layer.getWebLayerSharingDraft(). I am trying to replicate the "save web layer" functionality but it seems to incorporate more items than just the layer I am trying to update.
It is mimicking the Publish Web Layer functionality, so whatever is in your Map will be published to the feature service. Remove any layers or tables that you do not want published. If your target service has multiple layers I would look to the ArcGIS API for Python for updating/overwriting individual feature layers in a hosted feature service.