Automate overwrite web layer, feature class

36870
79
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

79 Replies
AmandaBishop2018
New Contributor III

Hello Jake,

Your script is awesome and I have successfully used it to updated a feature layer on my AGOL organization account.  The only issue I am facing now is to be able to preserve the "allow export data" option.

Please help!

Amanda

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi Amanda,

Try updating the following line to include the allow_exporting parameter:

arcpy.mp.CreateWebLayerSDDraft(mp, sddraft, sd_fs_name, ‘MY_HOSTED_SERVICES’, ‘FEATURE_ACCESS’,", True, True, allow_exporting=True)
0 Kudos
AmandaBishop2018
New Contributor III

Worked perfectly!  Another questions... since I'm fairly new to automating this task... How would I preserve the thumbnail pic?

0 Kudos
AmandaBishop2018
New Contributor III

And also... with the allow export option.. it seems to allow the export but not to a fgdb format?

0 Kudos
AmandaBishop2018
New Contributor III

I tried this update script to another copy of our parcel feature service called "TaxParcels".  I recieved an error this time.

0 Kudos
Jaivik_P
New Contributor III

Hi Amanda,

I not sure whether you got your issue solved or not. But if not yet, then I think the error suggests that you should add a layer on a map. for example you can use map.addLayer (layer, "TOP") , and then try creating service definition file.

0 Kudos
RoryBennison
New Contributor III

I  had this issue and found that I actually had 3 maps saved in my projects and the top map was empty, removed the other two and it worked perfectly

0 Kudos
ToddMcNeil
New Contributor

@JakeSkinner I use Pro to create the hosted feature service with the Export Data checked on.  When I update the FS using your code with the allow_exporting=True added.  It is removing the Download capability for the hosted feature service from my Portal Sites page.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

@ToddMcNeilI would recommend using ArcGIS Pro's Delete Rows and Append tools, rather than an overwrite.  These tools will work the same on a feature service just as if it were a feature class.

0 Kudos
ClintonCooper2
New Contributor III

I am having the following issue with arcpy.mp.CreateWebLayerSDDraft as I try to publish from my client to our companies hosted gis enterprise server/portal:

Traceback (most recent call last):
File "<string>", line 35, in <module>
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

Any idea what would be causing this?  Here is the code I am running:

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

### Start setting variables
# Set the path to the project
prjPath = r"C:\My Output Data\Projects\Dashboard\Dashboard.aprx"

# Update the following variables to match:
# Feature service/SD name in arcgis.com, user/password of the owner account
sd_fs_name = "Covid19 Daily Transactions Map"
portal = "https://sdc01acgicpw01s.corp..com/portal/" # Can also reference a local portal
user = "     "
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("{} 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)

0 Kudos