HOW TO

List all items of specified users using the PortalPy module

Last Published: April 25, 2020

Summary

Items such as files, web items, and applications can be added to Portal for ArcGIS for sharing through the My Content tab. For more information, refer to the following web help document, Portal for ArcGIS: Add items.

This article provides a sample script to list all available portal items for Portal for ArcGIS or ArcGIS Online for specified users in the organization. The script uses the PortalPy module and portal sharing API search. The script creates a .csv file that includes the information of items, for example, owner, title, id, type, and URL. The following instructions provided describe how to list all portal items of specified users in Portal for ArcGIS using the PortalPy module.

Procedure

Prior to running the script, users must configure the PortalPy module on the machine. For more information, refer to the following web help document: Portal for ArcGIS: Configuring the PortalPy module on your machine.

Use the following script to list all available portal items in Portal for ArcGIS. The script imports the PortalPy module, requests the login credentials for the specified Portal URL used, and uses for loop in reference to the username of specified users, and lists the items information held by the username.
#!/usr/bin/python
import portalpy, json, urllib, urllib2, sys, os, getpass, pprint

log_dict = []

# Update this segment
dir = r"C:\Users\xueh7833\Documents\ArcGIS\PythonStuff\PortalAdmin\portalpy"
portalUrl = "https://jing7.maps.arcgis.com"
portalAdminUser     = "******"
portalAdminPassword = "******"

# Create csv file and write the header in csv
log_path = os.path.join(dir, 'log.csv')
logger = open(log_path,"w")
logger.write("owener,id,title,type,url" + "\n")

# For manual input
#portalAdminUser     = raw_input("Enter user name: ")
#portalAdminPassword = getpass.getpass("Enter password: ")
#portalAdminUser     = arcpy.GetParameterAsText(0)
#portalAdminPassword = arcpy.GetParameterAsText(1)

# Connect to Portal
portal = portalpy.Portal(portalUrl, portalAdminUser, portalAdminPassword)
pprint.pprint(portal)
token = portal.generate_token(portalAdminUser, portalAdminPassword)
print token
users = portal.search_users('')

# Loop users
for user in users :
    # Get user name and role
    userResp = portal.get_user(user['username'])
    #print userResp['role']
    #print user['username'] + ":  " + userResp['role']

    # Construct url to search items by user
    searchURL = "{0}/sharing/search".format(portalUrl)
    print searchURL
    query = "owner: {0}".format(user['username'])
    params = urllib.urlencode({"q": query,"f": "pjson", "token": token})
    request = urllib2.Request(searchURL, params)

    # Read information from item
    response = urllib2.urlopen(request)
    jsonResult = json.load(response)['results']
    print jsonResult
    if jsonResult:
        for n in jsonResult:
            #print n['title'] + "," + n['id'] + "," + n['type'] + "," + n['owner']

            # Write to csv based on search result json, here I returned owner, id, title, type, and url. There is more info you can write to csv as long as the key exists in result json.
            if n['url']:
                logger.write(n['owner'] + "," + n['id'] + "," + n['title'] + "," + n['type']+ "," + n['url'] + "\n")
            else:
                logger.write(n['owner'] + "," + n['id'] + "," + n['title'] + "," + n['type']+ "," + "\n")

    else:
        logger.write(user['username'] + ",,,," + "\n")

logger.close()
Note:
Update the code block under the comment, 'Update this segment' to specify the desired save folder location, portal URL, username, and password.
The example shown below demonstrates how the final code snippet looks like.
dir = r"C:\\ArcGIS "
portalUrl = https://jing7.maps.arcgis.com
portalAdminUser     = "TestingUsername"
portalAdminPassword = "TestingPassword"
Alternatively, if users intend to perform manual inputs for the portal username and password, comment or delete the predefined username and password, remove the hash symbol (#) in each line under the code segment below the comment, 'For manual input.' The example shown below demonstrates how the final code snippet looks like.
#portalAdminUser     = "TestingUsername"
#portalAdminPassword = "TestingPassword"

# Create csv file and write the header in csv
log_path = os.path.join(dir, 'log.csv')
logger = open(log_path,"w")
logger.write("owener,id,title,type,url" + "\n")

# For manual input
portalAdminUser     = raw_input("Enter user name: ")
portalAdminPassword = getpass.getpass("Enter password: ")
portalAdminUser     = arcpy.GetParameterAsText(0)
portalAdminPassword = arcpy.GetParameterAsText(1)

Article ID:000013094

Software:
  • Portal for ArcGIS

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic