English

How To: Export a list of portal users to CSV using ArcGIS API for Python

Summary

In some instances, it is necessary to export a list of organization users from Portal for ArcGIS. This can be done using the ListUsers command line utility, or Admin Tools for ArcGIS Online.

This article provides the workaround using ArcGIS API for Python to populate an empty CSV file with the list of portal users and the required properties, for example, the username, role, and email.

Procedure

  1. Import the necessary modules.
import csv  
from arcgis.gis import GIS
  1. Specify the user credentials, and connect to Portal for ArcGIS.
gis = GIS("https://<gisserver>.<domain>.com/arcgis", "username", "password", verify_cert=False)
  1. Create a new CSV file, and specify the file path.
outputFile = r"<full file path>"
  1. List all users.
users = gis.users.search()
  1. Specify the fields to be displayed, and populate the data to the output file.
with open(outputFile, 'w', newline='') as ResultFile:
    wr = csv.writer(ResultFile)
    header = 'Username', 'Role', 'Email'
    wr.writerow(header)
    for user in users:
        UserRow = user.username, user.role, user.email
        wr.writerow(UserRow)

The following shows the full script:

import csv 
from arcgis.gis import GIS

gis = GIS("https://test.esri.com:7443/arcgis", "username", "password", verify_cert=False)

outputFile = r"C:\Users\Desktop\test.csv"

users = gis.users.search()

with open(outputFile, 'w', newline='') as ResultFile:
    wr = csv.writer(ResultFile)
    header = 'Username', 'Role', 'Email'
    wr.writerow(header)
    for user in users:
        UserRow = user.username, user.role, user.email
        wr.writerow(UserRow)

Related Information

Last Published: 10/14/2020

Article ID: 000015496

Software: ArcGIS API for Python 1.8.2, 1.8.1, 1.8.0, 1.7.1, 1.7.0, 1.6.1, 1.6, 1.5.3, 1.5.2, 1.5.1, 1.5, 1.4.1, 1.4, 1.3, 1.2.4, 1.2.3, 1.2.2, 1.2.1, 1.2, 1.0.1, 1.0 Portal for ArcGIS 10.8.1, 10.8, 10.7.1, 10.7, 10.6.1, 10.6