Select to view content in your preferred language

ImportDocument for Portal Item (.mapx)

159
3
Jump to solution
3 weeks ago
HF_LBD
by
New Contributor II

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.

HF_LBD_0-1720166529197.png

The error:

HF_LBD_1-1720166963403.png

Thanks for any ideas.

Greetings from Germany

Holger

0 Kudos
1 Solution

Accepted Solutions
HaydenWelch
Occasional Contributor II

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)

 

Link to the full documentation for the GIS module 

View solution in original post

0 Kudos
3 Replies
HaydenWelch
Occasional Contributor II

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)

 

Link to the full documentation for the GIS module 

0 Kudos
HF_LBD
by
New Contributor II

Hi Hayden,

Thanks a lot. This works well.

Only correction: it has to be file_name in line 17.

Greetings

Holger

HaydenWelch
Occasional Contributor II

Good catch, made the change so it doesn't cause issues for anyone else that might be reading this in the future

0 Kudos