Error with function: arcpy.mp.CreateWebLayerSSDraft

2027
6
02-13-2018 06:00 AM
ZecsQuébec
New Contributor III

I am trying to wirte a script that will update automatically my hosted feature services from ArcGIS Pro to ArcGIS Online via a python script. I am new to python but found this post that gives a detailed description of the script needed. 

Updating your hosted feature services with ArcGIS Pro and the ArcGIS API for Python | ArcGIS Blog 

I am trying to update the script to my needs so I changed the project path, sd_fs_name, user and password.  After running:

arcpy.mp.CreateWebLayerSDDraft(mp, sddraft, sd_fs_name, 'MY_HOSTED_SERVICES', 'FEATURE_ACCESS','', True, True)

I get this error that I cannot figure out:

Traceback (most recent call last):
File "<string>", line 32, in <module>
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\utils.py", line 191, in fn_
return fn(*args, **kw)
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

In the blog comments others have had the same problem but no solution has yet been offered there so I am hoping I might get more information here. Running on ArcGIS Pro 2.1.

Thanks in advance!

6 Replies
NicEverdell
Esri Contributor

Hi Zecs Quebec,

I have also seen this issue, when running a script from within ArcGIS Pro.

I have found that if I am signed in to ArcGIS Online and it is my Active Portal within Pro, the issue does not seem to occur.

Thanks,

Nic

ZecsQuébec
New Contributor III

Thank you very much for your reply. I though I was connected to Online and that it was my active portal but maybe not - I'll check! I finally fixed my problem by specifiyng the path to my project instead of having relPath = sys.path[0] (line 24 in the code sample from the blog). 

Reply
0 Kudos
SantiagoGarcia1
New Contributor

I am having the same issue with arcpy.mp.CreateWebLayerSDDraft.

Can you please post the code you used in place of relPath = sys.path[0]

Thank you.

Reply
0 Kudos
ZecsQuébec
New Contributor III

Hi,

Here is the code that I used, I hope it helps you resolve your issue! Note that I replaced the actual username and password with XXXX. I am a beginner in Python but if you have questions I'll do my best to help   "Public_lac" is my map in my ArcGIS Pro Projet. Since I have many maps in my project I had to find wich "number" was associated to "Public_lac" because I could not call the map by name so this is the line: mp = prj.listMaps("Public_lac")[0] .

##Upload - overwrite Public_lac
import arcpy
import os, sys
from arcgis.gis import GIS

##Start setting variables
#Set path to the project
prjPath = "C:\\Users\\Proprio\\Desktop\\ArcGISOnline_FichierTravail\\AppWeb_ZecsQuébec.aprx"

#Feature service/SD name in arcgis.com, user/password of the owner account
sd_fs_name= "Public_lac"
portal = "http://www.arcgis.com"
user = "XXXX"
password = "XXXX"

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

### End setting variables

# Local paths to create temporary content
relPath = 'C:\\Users\\Proprio\\Documents\\ArcGIS\\Projects'
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("Public_lac")[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))

ConradSchaefer__DOIT_
New Contributor II

We have an automated process that overwrites a hosted feature layer in ArcGIS Online, like the blog post referenced above. We encountered an issue somewhere around February 4, 2019 involving a RuntimeError exception in the call to arcpy.mp.CreateWebLayerSDDraft() that prints out nothing meaningful when handled in a try/except. 

 The portion of the code that was failing is as follows...

try:
arcpy.mp.CreateWebLayerSDDraft(map_or_layers=arcpro_map,
out_sddraft=sd_draft_filename,
service_name="Elected_Officials",
server_type="MY_HOSTED_SERVICES",
service_type="FEATURE_ACCESS",
folder_name="MD Elect",
overwrite_existing_service=True,
copy_data_to_server=True,
enable_editing=False,
allow_exporting=False,
enable_sync=False,
summary=None,
tags=None,
description=None,
credits=None,
use_limitations=None)
except RuntimeError as rte:
print(f"Runtime Error: {rte}")
exit()
else:
arcpy.StageService_server(in_service_definition_draft=sd_draft_filename,
out_service_definition=sd_filename)

Outside of a try/except this is the error output...

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

After much trial and error we figured out that the space in the folder name value was causing the issue. The process had been running for months with no issue using the string value "MD Elect". It looks like something in the underlying arcpy code was changed. We revised our ArcGIS Online folder name to eliminate the space, so that the destination folder matched the string we were forced to pass, and gave the function "MD_Elect" and the process runs again. We also tried "MD%20Elect", thinking it might be some web related issue, even though the function of interest doesn't make a web call, but it failed with the RuntimeError. The default of None and also a blank string "" worked, as a side note.

So, for our anonymous RuntimeError exception thrown by arcpy.mp.CreateWeblayerSDDraft the solution was to eliminate spaces in the string value passed to the folder_name keyword parameter. 

Error with function: arcpy.mp.CreateWebLayerSSDraft

Matthew Sokol (DOIT)

Python

GIS Developers

MichaelKellyTeck
Occasional Contributor

Also ensure the out_sddraft folder specified as a parameter exists.

Reply
0 Kudos