I am working on a ArcPy script *1 to update some existing layers that I have hosted on AGOL. I have been able to get everything running except for updating the summary, description and tags. The data is being fed into CreateWebLayerSDDraft()
in the tags, description and summary parameters, but still nothing. EDIT: To clarify. This code runs without errors, but the Description, Tags and Summary are not updated
I tried to add another publish statement for hostedFS
, but I kept getting a revolving door of errors *2. I think that it is either related to the way that I wrote the update() statement or something with the publish() statement.
EDIT: These errors do not show up when I run the following script. They only show up when I try to publish() the Hosted Feature Service.
I am hoping to get this up and running, but I cannot for the life of me figure out what is going on.
*1
list_of_fc = [["MetroBusRoutes",
f"Current MetroBus Routes for the Metro Saint Louis Transit system as of {date}.",
f"Current MetroBus Routes for the Metro Saint Louis Transit system as of {date}. n\Sign: {sign}, n\Date Published: {date} n\Projection: WGS1984",
["metro","routes","transit","metrobus","bus","MetroBusStops"],
],
for fc in list_of_fc:
# Local paths to create temporary content
print(f"Start of: {fc[0]}")
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")
ap.env.overwriteOutput = True
prj = ap.mp.ArcGISProject(prjPath)
mp = prj.listMaps(fc[0])[0]
hosted_file_name = f"{fc[0]}_REGISTERED"
ap.mp.CreateWebLayerSDDraft(map_or_layers=mp,
out_sddraft=sddraft,
service_name=hosted_file_name,
server_type='HOSTING_SERVER',
service_type='FEATURE_ACCESS',
folder_name=folder,
overwrite_existing_service=True,
copy_data_to_server=True,
summary=fc[1],
description=fc[2],
tags=fc[3])
ap.StageService_server(sddraft, sd)
print(f"Connecting to {portal}")
gis = GIS(portal, user, password) # user, password
# Find the SD, update it, publish /w overwrite and set sharing and metadata
print("Search for original SD on portal ...")
print(f"Query: {hosted_file_name}")
sdItem = gis.content.search(query=hosted_file_name,
item_type="Service Definition")[0]
hostedFS = gis.content.search(query=hosted_file_name,
item_type="Feature Layer Collection")[0]
# sdItem = gis.content.get(fc[4])
print(' ')
print("sdItem:")
print(sdItem)
print(f"Found SD: {sdItem.title}, ID: {sdItem.id} n Uploading and overwriting ...")
print(' ')
print("hostedFS:")
print(hostedFS)
print(f"Found SD: {hostedFS.title}, ID: {hostedFS.id} n Uploading and overwriting ...")
print(' ')
sdItem.update(data=sd)
hostedFS.update(item_properties={'tags': fc[3],'description': fc[2]})
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(f"Finished updating: {fs.title} – ID: {fs.id}")
*2 There are 2 possible error codes that keep switching back and forth when I run this.
EDIT: The following errors only show up when I run a publish() with the hosted Feature Service.
hfs = hostedFS.publish(overwrite=True)
if shrOrg or shrEveryone or shrGroups:
print("Setting sharing options ...")
fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)
print(f"Finished updating: {fs.title} – ID: {fs.id}")
#ERROR CODES
#ERROR 1
Traceback (most recent call last):
Finished updating: MetroBusRoutes_REGISTERED – ID: 08a5bfc4fb974e94b03cfe24627cc43c
File "C:/Users/wkjenkins/local_dev/metro-st-louis/Stops_and_Lines_Creation.py", line 555, in <module>
updatWebLayers(pattern_file_date)
File "C:/Users/wkjenkins/local_dev/metro-st-louis/Stops_and_Lines_Creation.py", line 230, in updatWebLayers
fs = item.publish(overwrite=True)
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py", line 8327, in publish
raise ValueError("A file_type must be provide, data format not recognized")
ValueError: A file_type must be provide, data format not recognized
#ERROR 2
User cant overwrite this service, using this data, as this data is already referring to another service.
Traceback (most recent call last):
File "C:/Users/wkjenkins/local_dev/metro-st-louis/Stops_and_Lines_Creation.py", line 555, in <module>
updatWebLayers(pattern_file_date)
File "C:/Users/wkjenkins/local_dev/metro-st-louis/Stops_and_Lines_Creation.py", line 230, in updatWebLayers
fs = item.publish(overwrite=True)
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py", line 8490, in publish
folder, buildInitialCache)
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\_impl\portalpy.py", line 372, in publish_item
resp = self.con.post(path, postdata, files)
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\_impl\connection.py", line 1177, in post
self._handle_json_error(resp_json['error'], errorcode)
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\_impl\connection.py", line 1198, in _handle_json_error
raise RuntimeError(errormessage)
RuntimeError: User cant overwrite this service, using this data, as this data is already referring to another service.
(Error Code: 500)
CreateWebLayerSDDraft
?. The second error might be related with this note ArcGIS does not permit overwriting if you published multiple hosted feature layers from the same data item. from arcgis docgis.content.search
returning what you expect it to return)