Automate overwrite web layer, feature class

36111
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
JakeSkinner
Esri Esteemed Contributor

Hi Einar,

I recently implemented the script in your 3rd option above.  I found this easiest.  Save your layer in a Pro project and then you can run the below:

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

### Start setting variables
# Set the path to the project
prjPath = r"C:\PROJECTS\NightlyUpdates\NightlyUpdates.aprx"

# Update the following variables to match:
# Feature service/SD name in arcgis.com, user/password of the owner account
sd_fs_name = "MyPublicMap"
portal = "http://www.arcgis.com" # Can also reference a local portal
user = "UserName"
password = "p@sswOrd"

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

### End setting variables

# Local paths to create temporary content
relPath = os.path.dirname(prjPath)
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))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
EinarDørheim1
New Contributor III

Hi, firstly thansk for the quick and explainatory answer. However I'm getting an error when i try to run the script, not understanding how to solve it: 

Any tips?

Creating SD file




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

ExecuteError                              Traceback (most recent call last)
<ipython-input-3-540a66d6dc34> in <module>()
     32 mp = prj.listMaps()[0]
     33 arcpy.mp.CreateWebLayerSDDraft(mp, sddraft, sd_fs_name, 'MY_HOSTED_SERVICES', 'FEATURE_ACCESS','', True, True)
---> 34 arcpy.StageService_server(sddraft, sd)
     35 
     36 print("Connecting to {}".format(portal))

C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\server.py in StageService(in_service_definition_draft, out_service_definition, staging_version)
    846         return retval
    847     except Exception as e:
--> 848         raise e
    849 
    850 @gptooldoc('UploadServiceDefinition_server', None)

C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\server.py in StageService(in_service_definition_draft, out_service_definition, staging_version)
    843     from arcpy.arcobjects.arcobjectconversion import convertArcObjectToPythonObject
    844     try:
--> 845         retval = convertArcObjectToPythonObject(gp.StageService_server(*gp_fixargs((in_service_definition_draft, out_service_definition, staging_version), True)))
    846         return retval
    847     except Exception as e:

C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py in <lambda>(*args)
    504         val = getattr(self._gp, attr)
    505         if callable(val):
--> 506             return lambda *args: val(*gp_fixargs(args, True))
    507         else:
    508             return convertArcObjectToPythonObject(val)

ExecuteError: ERROR 999999: Error executing function.
Failed to execute (StageService).

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Looks like it's erroring when trying to create the SD file.  Double-check to make sure you have the correct path to your aprx file.

0 Kudos
EinarDørheim1
New Contributor III

figured it out, the problem was with:

relPath = sys.path[0]

Changed sys.path[0] to an absolute path and it worked like a charm!

0 Kudos
BenBaker1
Occasional Contributor

Hello, I am getting a different error when I run it in the Python Window in ArcGIS Pro:

### End setting variables

# Local paths to create temporary content
relPath = sys.path[0]
sddraft = os.path.join(relPath, "CitizenConcerns_all.sddraft")
sd = os.path.join(relPath, "CitizenConcerns_all.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))
Creating SD file
Traceback (most recent call last):
File "<string>", line 33, 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

Tweed_ShireAdmin
New Contributor III

I am having the same error appear for me when running the script in Python from ArcGIS Pro. Did you get a solution as to why the error was coming and if so what was it. 

Any help would be appreciated

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Tweed Shire Admin‌ can you post the code you are executing?

0 Kudos
GlenShepherd
Esri Contributor

Hi Jake, I'm currently working with Tweed Shire Admin to try and resolve this issue.

Code is as follows:

- User is unable to run a python script that overwrites a feature layer in ArcGIS Online
- Script is fine, and I can run her script (even with her credentials included) from my ArcGIS Pro
- Issue is not specific to her machine
- Complete uninstall and reinstall did not resolve the issue.
- Error message cites Runtime error
- Script tested same problems in Python window of ArcGIS Pro and IDLE
- Replicated on another machine within the network
- Replicated on a laptop outside of the network
- Comprehensive uninstall and reinstallation had no effect
- Client cannot manually overwrite the feature service from Pro either
- Systems check revealed machine environment is fine for Pro
- There are no other feature services published from the same data

Same error received as bbaker_mhgis 

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi Glen

Client cannot manually overwrite the feature service from Pro either

If ArcGIS Pro is not working correctly, the code will most likely not work.  I would focus on getting Pro working, then circle back to the code.

0 Kudos