Hi
I'm trying to automate an update process for one of my hosted features in portal. So far I have:
1. Solved the automatic download and adjustment of the file in question. (using powershell/task scheduler)
2. exportet the python code that runs the "Geocode Addresses" tool on my file, saves it in a file gdb.
3. manually published the feature class with the "overwrite web layer" function.
My challange is to automate step 3, and connect it to step 2.
Please keep in mind that I'm completely new to scripting/python.
I'm currently using ArcGIS Pro 2.1.0, and have admin rights on the portal.
I have looked at thise sites, and think they describe partly how to solve my issue, I'm just not able to pick out the relevant parts and build a script that works for my particular problem..
community.esri.com "using python to overwrite a feature layer"
developers.arcgis.com python, overwriting feature layers
esri updating-your-hosted-feature-services-with-arcgis-pro-and-the-arcgis-api-for-python
Hey @JakeSkinner,
I made a script for the publication of feature services from Arcgis pro to Online. but what i did is publish all the layers separately, each layer has its own service. to do this i used a loop, now i want to update the layers i had posted, if you have any idea how to do it please !
This is the script that i used :
import arcpy
import os
# Sign in to portal
arcpy.SignInToPortal('https://.arcgis.com', 'UserName', 'Password')
in_server = "https://*********.fr/server/"
# Set output file names
outdir = r"F:\Projet_ArcGisPro\TEST\Output"
# Reference map to publish
aprx = arcpy.mp.ArcGISProject(r"F:\Projet_ArcGisPro\TEST\TEST.aprx")
m = aprx.listMaps()[0]
lyrs = m.listLayers()
for lyr in lyrs:
service = lyr.name
sharing_draft = m.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", service, lyr)
sharing_draft.summary = lyr.name
sharing_draft.tags = lyr.name
sharing_draft.description = lyr.name
sharing_draft.credits = lyr.name
sharing_draft.useLimitations = lyr.name
sharing_draft.portalFolder = "SERVICES_OpenData"
print(service)
sddraft_filename = service + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
# Create Service Definition Draft file
sharing_draft.exportToSDDraft(sddraft_output_filename)
# Stage Service
sd_filename = service + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)
arcpy.StageService_server(sddraft_output_filename, sd_output_filename)
# Share to portal
print("Uploading Service Definition...")
arcpy.UploadServiceDefinition_server(sd_output_filename, in_server,"","","","","","","SHARE_ONLINE","PUBLIC","SHARE_ORGANIZATION")
print("Successfully Uploaded service.")
Thank you for your help 🙂
Hi @inolaroch, are you looking to update the data within these services? If the schema has not changed, I would recommend using a truncate/append method. See the following document:
This will allow you to maintain any pop-ups you may have configured in web maps.
@JakeSkinner No, I modified the layers manually on Arcgis Pro, now I want to overwrite the old services that I had previously published...
@inolarochare you publishing the services to ArcGIS Online, or to ArcGIS Server and sharing to ArcGIS Online? Try adding the following to your script:
sharing_draft.overwriteExistingService = True
what i want to do is reuse the script below but i want it to search each service and overwrite it with the new one...because my first script it publishes the layers separately as Feature Services.
import arcpy
import os, sys
from arcgis.gis import GIS
# Start setting variables
# Set the path to the project
prjPath = r"D:\Sample Data\SampleWorking\SampleWorking.aprx"
# Update the following variables to match
sd_fs_name = "ShereeTest"
portal = "https://www.arcgis.com/"
user = "enter_username"
password = "enter_password"
# Set sharing options
shrOrg = True
shrEveryone = True
shrGroups = ""
# End setting variables
# Local paths to create temporary content
relPath = r"C:\GIVE_A_FILE_DIRECTORY"
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)
arcpy.StageService_server(in_service_definition_draft=sddraft,
out_service_definition=sd)
print("Connecting to {}".format(portal))
gis = GIS(portal, user, password)
# Find the sd, update it, publish it with 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 sheEveryone or sheGroups:
print("Setting sharing options...")
fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)
print("Finished updateing: {} - ID: {}".format(fs.title, fs.id))
@inolaroch Try the following:
import arcpy
import os, sys
from arcgis.gis import GIS
# Start setting variables
# Set the path to the project
prjPath = r"D:\Sample Data\SampleWorking\SampleWorking.aprx"
# Update the following variables to match
sd_fs_name = "ShereeTest"
portal = "https://www.arcgis.com/"
user = "enter_username"
password = "enter_password"
# Set sharing options
shrOrg = True
shrEveryone = True
shrGroups = ""
# End setting variables
print("Connecting to {}".format(portal))
gis = GIS(portal, user, password)
# Local paths to create temporary content
relPath = r"C:\GIVE_A_FILE_DIRECTORY"
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]
lyrs = mp.listLayers()
for lyr in lyrs:
arcpy.mp.CreateWebLayerSDDraft(map_or_layers=mp,
out_sddraft=sddraft,
service_name=lyr.name,
server_type="HOSTING_SERVER",
service_type="FEATURE_ACCESS",
folder_name="SERVICES_OpenData",
overwrite_existing_service=True,
copy_data_to_server=True)
arcpy.StageService_server(in_service_definition_draft=sddraft,
out_service_definition=sd)
# Find the sd, update it, publish it with 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 sheEveryone or sheGroups:
print("Setting sharing options...")
fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)
print("Finished updating: {} - ID: {}".format(fs.title, fs.id))
Hey @JakeSkinner , i get the RuntimeError :
Hey @JakeSkinner, I modified some things and it worked perfectly, Thank you for your help 🙂
Hi,
I got the same run time error. Could you share your modification? Thanks!
so after further investigation I found the solution @ here
relPath = r"C:\Users\user\Documents\ArcGIS\Projects"