Hi,
I'm using a python script in which I imported .mapx-files succesfully from a server.
Now I need the same script to import the .mapx from our portal, but it seems I get the URL wrong, since the tool throws an OS Error.
The first 3 lines show the working part of the script, setting the document path to NK.mapx on our server.
The last 2 lines show two different document paths to the portal item I tried. The first one is the same which is used when importing the .mapx-file in ArcGIS Pro via the menu.
The error:
Thanks for any ideas.
Greetings from Germany
Holger
Solved! Go to Solution.
It could be that your portal requires authentication, using the GIS module to interact with portal items might be necessary in your case. It's not totally clear to me if the importDocument method for .mp allows network paths. You could try signing in and downloading the map to a tempfolder then adding it:
import arcpy
import os
import tempfile
from arcgis.gis import GIS, ContentManager, Item
def connect_to_portal(url: str = None, username: str = None, password: str = None) -> GIS:
return GIS(url, username, password)
def get_manager(portal: GIS) -> ContentManager:
return portal.content
def get_item_by_id(manager: ContentManager, item_id: str) -> Item:
return manager.get(item_id)
def download_item(item, output_folder: os.PathLike) -> os.PathLike:
item.download(save_path=output_folder, file_name=item.name)
return os.path.join(output_folder, item.name)
if __name__ == "__main__":
aprx = arcpy.mp.ArcGISProject("CURRENT")
tempfolder = tempfile.gettempdir()
# Connect to the portal
portal = connect_to_portal("https://www.arcgis.com", "username", "password")
# Get portal content manager
manager = get_manager(portal)
# OPTIONAL use search to find the item
# items = manager.search("title:Item Title")
# Get item by id
item = get_item_by_id(manager, "<item_id>")
# download the item to a temporary folder
map_item = download_item(item, tempfolder)
# import the item into the current project
aprx.importDocument(map_item)
It could be that your portal requires authentication, using the GIS module to interact with portal items might be necessary in your case. It's not totally clear to me if the importDocument method for .mp allows network paths. You could try signing in and downloading the map to a tempfolder then adding it:
import arcpy
import os
import tempfile
from arcgis.gis import GIS, ContentManager, Item
def connect_to_portal(url: str = None, username: str = None, password: str = None) -> GIS:
return GIS(url, username, password)
def get_manager(portal: GIS) -> ContentManager:
return portal.content
def get_item_by_id(manager: ContentManager, item_id: str) -> Item:
return manager.get(item_id)
def download_item(item, output_folder: os.PathLike) -> os.PathLike:
item.download(save_path=output_folder, file_name=item.name)
return os.path.join(output_folder, item.name)
if __name__ == "__main__":
aprx = arcpy.mp.ArcGISProject("CURRENT")
tempfolder = tempfile.gettempdir()
# Connect to the portal
portal = connect_to_portal("https://www.arcgis.com", "username", "password")
# Get portal content manager
manager = get_manager(portal)
# OPTIONAL use search to find the item
# items = manager.search("title:Item Title")
# Get item by id
item = get_item_by_id(manager, "<item_id>")
# download the item to a temporary folder
map_item = download_item(item, tempfolder)
# import the item into the current project
aprx.importDocument(map_item)
Good catch, made the change so it doesn't cause issues for anyone else that might be reading this in the future