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
I've been trying @AmandaBishop2018 's script above with @NicolasMarecos edit but keep running into issues.
Script: as entered
import arcpy
import os, sys
from arcgis.gis import GIS
### Start setting variables
# Set the path to the project
prjPath = r"Z:\ArcMap\UpdateHosted\UpdateHosted.aprx"
# Update the following variables to match:
# Feature service/SD name in arcgis.com, user/password of the owner account
sd_fs_name = "Test_Feature"
portal = "https://parkcity.maps.arcgis.com" # Can also reference a local portal
user = "devin.boyle"
password = "********"
# Set sharing options
shrOrg = True
shrEveryone = False
shrGroups = ""
### End setting variables
# Local paths to create temporary content
relPath = sys.path[0]
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(mp, sddraft, sd_fs_name, "MY_HOSTED_SERVICES", "FEATURE_ACCESS","", True, True, allow_exporting=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(query="title:"+ sd_fs_name + " AND owner: " + 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)
But I always end up with these results:
IndentationError Traceback (most recent call last)
File C:\Program Files\Arcgis\Pro\bin\Python\envs\arcgispro-py3\lib\ast.py, in parse:
Line 35: return compile(source, filename, mode, PyCF_ONLY_AST)
IndentationError: expected an indented block (<string>, line 49)
I'm also having trouble overwriting a hosted feature service on our Enterprise Portal. I have an sde dataset that is updated weekly that we're using for a Field Maps app. I need to automate the update of the hosted feature layer. When I run the script below, I get the error: This service definition file was created for an ArcGIS Server site and must be uploaded directly to an ArcGIS Server site through ArcGIS Server Manager or ArcMap.
(Error Code: 400)
import arcpy
import os, sys
from arcgis.gis import GIS
prjPath = r"M:\PROJECTS\Projects_2020_21\Units_For_Field_Maps\Units_For_Field_Maps.aprx"
service = "Units_For_Field_Maps"
portal = "our enterprise portal"
user = "user"
password = "password"
shrOrg = True
shrEveryone = False
shrGroups = ""
relPath = r'C:\Temp
sddraft = os.path.join(relPath, "Units_For_Field_Maps.sddraft")
sd = os.path.join(relPath, "Units_For_Field_Maps.sd")
print("Creating SD file")
arcpy.env.overwriteOutput = True
prj = arcpy.mp.ArcGISProject(prjPath)
m = prj.listMaps()[0]
arcpy.mp.CreateWebLayerSDDraft(m, sddraft, service, "MY_HOSTED_SERVICES", "FEATURE_ACCESS","", True, 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(service, '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))
@LeviCecilinstead of overwriting the hosted feature service, you could run ArcGIS Pro's Delete Rows and Append tools on the feature service. These two tools could be executed in a model, or you could export the model to a python script and run the script as a scheduled task.
Sure, I could do that. But if I can right click and overwrite the hosted feature service in Pro, shouldn’t I be able to do the same from Python?
is there a way to have the hosted feature service reflect the changes in the sde data automatically? I’d rather simplify this process.
@LeviCecilyes, the database need to be registered with ArcGIS Server, and you need to publish the service as a referenced service.
It is registered with the server, and I've published this way. It publishes as a regular feature service, though. Not a referenced service.
Hi everyone,
Wondering if it is possible to overwrite a feature service in AGOL using this script. I new to using python in ArcGIS Pro and gather that this only works with portal..? I see this might be a better option: Overwrite ArcGIS Online Feature Service using Trun... - Esri Community
I am trying to use the following code to update my hosted feature service. I keep on getting the error that .sddraft file does not exist. Could some one please suggest what is wrong with this code.
import arcpy
import os, sys
from arcgis.gis import GIS
prjPath = r"C:\Users\axb9294\Desktop\RoadofWichita.aprx"
sd_fs_name = "Road_Wichita"
portal = "http://myorg-arcgis.maps.arcgis.com" # Can also reference a local portal
user = "........................"
password = "..............."
shrOrg = True
shrEveryone = True
shrGroups = ""
relPath = os.path.dirname(prjPath)
sddraft = os.path.join(relPath, "Road_Wichita.sddraft")
sd = os.path.join(relPath, "Road_Wichita.sd")
print("Creating SD file")
arcpy.env.overwriteOutput = True
prj = arcpy.mp.ArcGISProject(prjPath)
mp = prj.listMaps()[0]
print(mp)
arcpy.sharing.CreateSharingDraft(mp, sd_fs_name, 'MAP_SERVICE', 'STANDALONE_SERVER')
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))
Image shows the error I get. I have tried different ways of assigning the path but that doesn't work.
I am trying to overwrite a feature service using following code. I keep on getting a error ( attached picture below). Can anyone help me fix this error.
Script I am using (source: https://support.esri.com/en/technical-article/000023164)
import arcpy
import os, sys
from arcgis.gis import GIS
prjPath = r"C:\Users\axb9294\Desktop\RoadofWichita.aprx"
sd_fs_name = "Road_Wichita"
portal = "http://org-arcgis.maps.arcgis.com" # Can also reference a local portal
user = "................"
password = "......................................"
shrOrg = True
shrEveryone = True
shrGroups = ""
relPath = os.path.dirname(prjPath)
sddraft = os.path.join(relPath, "Road_Wichita.sddraft")
sd = os.path.join(relPath, "Road_Wichita.sd")
print("Creating SD file")
arcpy.env.overwriteOutput = True
prj = arcpy.mp.ArcGISProject(prjPath)
mp = prj.listMaps()[0]
print(mp)
arcpy.sharing.CreateSharingDraft(mp, sd_fs_name, 'MAP_SERVICE', 'STANDALONE_SERVER')
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))
The error I get after executing the code is below. I have tried providing full path for sddraft and sd file, but error still appears.
@anilbaral99I always get cautious when overwriting a feature service. As an alternative, you may want to try the following script as a workaround: