3

I'm using ArcPy and python 3.5 to connect to a few different instances of ArcGIS portal.

The goal is to produce a report listing the maps, layers, and datasources that each portal instance has to compare differences between them.

My problem is, I can't figure out how to retrieve or list the maps from python. I can connect to the portals and output the data from the GetPortalDescription function but I'm not sure where to go next.

import arcpy
import arcgis
import json
from urllib.parse import urlparse
from arcgis.gis import GIS

def parse_url(url):
    parsed_uri = urlparse( url)
    hostname=parsed_uri.hostname    
    return hostname

def output_portal(portalUrl, portalAdminUser, portalAdminUserPassword):
    #gis = GIS(portalUrl, portalAdminUser, portalAdminUserPassword)
    gis=GIS()
    url = arcpy.GetActivePortalURL()
    print("Current Portal: "+url)
    domain=parse_url(url)
    portalDesc =arcpy.GetPortalDescription()
    outfile=open(domain+".json","w")
    outfile.write(json.dumps(portalDesc, indent=4, sort_keys=True))
    outfile.close()
    # get all maps posted to the portal
    # for each map print all the layers in the portal 
2
  • 2
    Post your code. You just need to search the content. Use the arcgis python api, not arcpy. Mar 23, 2018 at 20:23
  • @BenSNadler Done. I guess I was barking up the wrong tree. I thought I had to use arcpy to talk to the portal.
    – gnarbarian
    Mar 23, 2018 at 20:35

1 Answer 1

2

With the ArcGIS Python API from the help

The output of this is a dictionary of all items (source_items_by_id).

Do with them as you wish:

from arcgis.gis import GIS
source = GIS("https://host.esri.com/portal", "adminuser", "adminpassword")
# Query Portal for all users, ignore admin and system accounts
source_users = source.users.search('!esri_ & !admin')
source_items_by_id = {}
for user in source_users:
    num_items = 0
    num_folders = 0
    print("Collecting item ids for {}".format(user.username), end="\t\t")
    user_content = user.items()

# Get item ids from root folder first
for item in user_content:
    num_items += 1
    source_items_by_id[item.itemid] = item

# Get item ids from each of the folders next
folders = user.folders
for folder in folders:
    num_folders += 1
    folder_items = user.items(folder=folder['title'])
    for item in folder_items:
        num_items += 1
        source_items_by_id[item.itemid] = item

print("Number of folders {} # Number of items {}".format(str(num_folders), str(num_items)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.