Automate overwrite web layer, feature class

36094
78
06-14-2018 12:53 AM
EinarDørheim1
New Contributor III

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

78 Replies
BenYeager
New Contributor

Hello Glen,

   I am receiving the same runtime error above. I have tried all of the items you mentioned. Were you able to resolve this issue?

Thank you!

   Ben

0 Kudos
GlenShepherd
Esri Contributor

Hi Ben, yes I was able to figure out the issue.

sys.path[0] is a Python executable that refers to the directory of the script being run. Seeing as we were copying the script into the Python window, it doesn't have a saved directory, so we needed to give it one. i.e. r"D:\BigFolder\LittleFolder"

 This is the script I used, modified from the original one in this thread:

ShereePythonExample2

Here's the text if you'd like to copy it. Obviously you'll need to update all of your own parameters:

--------------------------------------------------------------------------------------------------------------------------------

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))

BenYeager
New Contributor

Thank you Glen!

0 Kudos
AndrewThompsonTRC
New Contributor II

UPDATE

I had to delete the WebUpdate.sd and WebUpdate.sddraft files. The script wasn't able to overwrite those 2 files for some reason. If anyone has ideas to why that might be the case that would be great to know.

 

Hi,

I changed the relPath = r"C:\GIVE_A_FILE_DIRECTORY" to a local folder and still getting this error.

Creating SD file
Traceback (most recent call last):
File "C:/ScheduledScripts/TRC/updatewebmap1.py", line 33, in <module>
arcpy.mp.CreateWebLayerSDDraft(mp, sddraft, sd_fs_name, 'MY_HOSTED_SERVICES', 'FEATURE_ACCESS','', True, True)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\mp.py", line 105, in CreateWebLayerSDDraft
return _convertArcObjectToPythonObject(arcgisscripting._mapping.CreateWebLayerSDDraft(*_gp_fixargs([map_or_layers, out_sddraft, service_name, server_type, service_type, folder_name, overwrite_existing_service, copy_data_to_server, enable_editing, allow_exporting, enable_sync, summary, tags, description, credits, use_limitations], True)))
RuntimeError
>>>

The script was working for over a year until last month. Only change is updating ArcPro to 2.7.1. I successfully overwrote the layer manually through ArcPro.

Any insight would be great.

import arcpy
import os, sys
from arcgis.gis import GIS

### Start setting variables
# Set the path to the project
prjPath = r"Path of aprx"

# Update the following variables to match:
# Feature service/SD name in arcgis.com, user/password of the owner account
sd_fs_name = "NAME OF LAYER"
portal = "NAME OF PORTAL" # Can also reference a local portal
user = "NAME OF USER"
password = "PASSWORD"

# Set sharing options
shrOrg = False
shrEveryone = False
shrGroups = "Group"

### End setting variables

# Local paths to create temporary content
relPath = r"C:\ScheduledScripts\TRC"
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)
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))

0 Kudos
PatFelser
New Contributor II

Hello, I am also receiving the same error. I am not having any issues with Pro however, and can manually update from there just fine. I am currently working out of jupyter notebook if that helps at all. 

0 Kudos
WaiChan
New Contributor II

I am using Windows Authentication for the Portal. And it gives the "IndexError: list index out of range" error message when it reaches the line sdItem = gis.content.search("{} AND owner:{}".format(sd_fs_name, user), item_type="Service Definition")[0]. 

Here are my 4 lines for connecting to the Portal in the beginning of the script.

sd_fs_name = "<feature layer name>"
portal = "<Portal URL>"
user = ""
password = ""

Any help will be greatly appreciated!

0 Kudos
WaiChan
New Contributor II

Never mind. User error. It would help to have the feature layer already hosted in the Portal. Once the feature layer exists in Portal, the script overwrote the hosted feature layer successfully.

NomanSajjad
New Contributor

Hi, 

It is working but it is corrupting my data on portal. All the lines and attribute data went missing. Please advise

import arcpy
import os, sys
from arcgis.gis import GIS

### Start setting variables
# Set the path to the project
prjPath = r"Z:\GIS\CityInf\Asset Management\GIS_Projects\014 - ArcGIS Enterprise Spatially Assetic\Working\Spatially Assetic\Spatially Assetic.aprx"

# Update the following variables to match:
# Feature service/SD name in arcgis.com, user/password of the owner account
sd_fs_name = "Assetic_Kerbs"
portal = "http://fdf.maps.arcgis.com" # Can also reference a local portal
user = "m"
password = ""

# Set sharing options
shrOrg = True
shrEveryone = False
shrGroups = "Assets"

### End setting variables

# Local paths to create temporary content
relPath = r"C:\Users\nsajjad\Python"
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(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))

0 Kudos
HushamMohamed
Occasional Contributor

Hi  Jake Skinner Thank you for helpful script. which partially answer my Question Here

Please correct me if I am wrong.

1-  The script take the first Layer in ArcGIS Pro Map?

2- the script just overwrite the layer in 1. (How can I loop through all the layers and overwrite them.)

3- Rest of Layers are not overwritten. 

4- Map itself is not overwritten. 

Also How can I  include the configuration such like date Fields Time zone

How to schedule this script to run every day at 5:00 PM ex.

0 Kudos
YouthD
by
New Contributor

Is there a way to choose which folder to use within Portal? I have a map layers folder and a data download folder. I would only need to update the layers in the Data Download folder. Thank you

0 Kudos