I would like to create a python Script to automatically update my web map from my ArcPro project.
I have a ArcPro project that I manually update the web map right now.
I also used experience builder to create a user-friendly web map that uses the data from my published web map.
I have attached my first try at a script, but it doesn't work. Any help on how to fix the script would be appreciated.
1st Script: Double check you are signed in and correct portal connection
arcpy.server.UploadServiceDefinition(sd, portal_url)
print(arcpy.GetActivePortalURL())
Change raise Exception(f"Map '{map_name}' not found.")
to
sddraft = os.path.join(sharing_draft_folder, f"{service_name}.sddraft")sd = os.path.join(sharing_draft_folder, f"{service_name}.sd")
print(f"{service_name} published successfully!")
And for the Exception try,
except Exception:print("Publishing failed.")print(arcpy.GetMessages(0))print(arcpy.GetMessages(1))print(arcpy.GetMessages(2))raise
2nd script:
You are missing your drive letter, PROJECT = r"\GISdata\ArcGISPro_Projects\SamplePublishArcPly.aprx", something like, PROJECT = r"C:\GISdata\ArcGISPro_Projects\SamplePublishArcPly.aprx"
before C:\GIS\Logs, add
os.makedirs(r"C:\GIS\Logs", exist_ok=True)os.makedirs(OUTPUT_FOLDER, exist_ok=True)
Error Handling try
try:...except Exception as ex:logging.error(ex)logging.error(arcpy.GetMessages())raise
import arcpy import os aprx_path = r"C:\GISdata\ArcGISPro_Projects\SamplePublishArcPly.aprx" map_name = "SampleAutoPublish" service_name = "SampleAutoPublish" portal_url = "https://www.arcgis.com" username = "xyzx" password = "yyyy" sharing_draft_folder = r"C:\GIS\Publishing" description = "Sample Auto Publish Feature Layer" tags = "GIS, Caldwell County, Sample" arcpy.SignInToPortal(portal_url, username, password) aprx = arcpy.mp.ArcGISProject(aprx_path) maps = aprx.listMaps(map_name) if not maps: raise Exception(f"Map '{map_name}' not found.") map_obj = maps[0] sharing_draft = map_obj.getWebLayerSharingDraft( "HOSTING_SERVER", "FEATURE", service_name ) sharing_draft.summary = description sharing_draft.description = description sharing_draft.tags = tags sharing_draft.overwriteExistingService = True os.makedirs(sharing_draft_folder, exist_ok=True) sddraft = os.path.join( sharing_draft_folder, f"{service_name}.sddraft" ) sd = os.path.join( sharing_draft_folder, f"{service_name}.sd" ) try: print("Creating SDDraft...") sharing_draft.exportToSDDraft(sddraft) print("Staging service...") arcpy.server.StageService(sddraft, sd) print("Uploading service...") arcpy.server.UploadServiceDefinition( sd, "HOSTING_SERVER" ) print(f"{service_name} published successfully!") except Exception: print("Publishing failed.") print(arcpy.GetMessages(0)) print(arcpy.GetMessages(1)) print(arcpy.GetMessages(2)) raise
Also, Double check you are signed in and correct portal connection
arcpy.server.UploadServiceDefinition(sd, PORTAL)
import arcpy import os import datetime import logging PROJECT = r"C:\GISdata\ArcGISPro_Projects\SamplePublishArcPly.aprx" MAP_NAME = "SampleAutoPublish" SERVICE_NAME = "SampleAutoPublish" OUTPUT_FOLDER = r"C:\GIS\Publishing" LOG_FOLDER = r"C:\GIS\Logs" PORTAL = "https://www.arcgis.com" USERNAME = "your_username" PASSWORD = "your_password" os.makedirs(OUTPUT_FOLDER, exist_ok=True) os.makedirs(LOG_FOLDER, exist_ok=True) logging.basicConfig( filename=os.path.join(LOG_FOLDER, "Publish.log"), level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s" ) try: arcpy.SignInToPortal(PORTAL, USERNAME, PASSWORD) aprx = arcpy.mp.ArcGISProject(PROJECT) maps = aprx.listMaps(MAP_NAME) if not maps: raise Exception(f"Map '{MAP_NAME}' not found.") m = maps[0] sharing_draft = m.getWebLayerSharingDraft( "HOSTING_SERVER", "FEATURE", SERVICE_NAME ) sharing_draft.overwriteExistingService = True sddraft = os.path.join(OUTPUT_FOLDER, f"{SERVICE_NAME}.sddraft") sd = os.path.join(OUTPUT_FOLDER, f"{SERVICE_NAME}.sd") for f in [sddraft, sd]: if os.path.exists(f): os.remove(f) sharing_draft.exportToSDDraft(sddraft) arcpy.server.StageService(sddraft, sd) arcpy.server.UploadServiceDefinition( sd, "HOSTING_SERVER" ) logging.info(f"{SERVICE_NAME} successfully overwritten.") print(f"Completed: {datetime.datetime.now()}") except Exception as ex: logging.error(str(ex)) logging.error(arcpy.GetMessages()) print("Publish failed.") print(ex) print(arcpy.GetMessages())
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.