Select to view content in your preferred language

A quick overview of group membership in your AGOL organization

03-21-2019 02:35 PM
MVP Regular Contributor
4 10 2,548

Do you recognize this?

  • "Over time our ArcGIS Online organization has grown. We have quite a few users now and a considerable number of groups has been created. Can you please provide me with a quick overview of who is member of which group?"
  • "Yes, I can."
  • "OK - and please note: not only quick, but also in Excel. Is that possible?"
  • "Eh... yeah, that is also possible."

We are going to use the ArcGIS API for Python with and to get an overview of our organization. Next we compile a matrix - users along the y-axis and groups along the x-axis - with a 1 or a 0 to indicate wether the user is a member of the group or not. This matrix is being written to a CSV file.

And then the boss should be able to import this CSV into a colorful and manageable spreadsheet. With or without a little help.

Does the script below work for you? Just 'like' or 'share' if it does. And if you want to, you can just open your Jupyter Notebook to try it line by line.

## ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
## ArcGIS Online Management Information
## Script:
## Goal: to create an overview of group membership in your ArcGIS Online organisation
## Author: Egge-Jan Polle - Tensing GIS Consultancy
## Date: August 3, 2018
## ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# This script should be run within a specific ArcGIS/Python environment using the batch file below
# (This batch file comes with the installation of ArcGIS Pro)
# "C:\Program Files\ArcGIS\Pro\bin\Python\scripts\propy.bat"
import csv,os, sys
from arcgis.gis import GIS
from provide_credentials import provide_credentials

print('The script that is running: ' + __file__)
print('First you have to log in to ArcGIS Online')

# Log in
username, password = provide_credentials()
my_agol = GIS("", username, password)

print ("Start: "'%c'))

## Get all groups
my_groups =
## Optionally: have a look at all groups
## Optionally: count the number of groups

## Get all users
my_users = = 350) # The default of max_users = 100, so increase it if you have more
## Optionally: have a look at all users
## Optionally: count the number of users

## Create a list with all group titles
my_group_titles = []
for my_group in my_groups:

## Create a list with field names
fieldnames = []
fieldnames = ['USERNAME','EMAIL']
for title in my_group_titles:
## Optionally: have a look at the field names

## Create a CSV file with a matrix of the groups with their members
today ='%Y%m%d')
fname = 'AGOL_Group_Membership'+today+'.csv'
except OSError:
outfile = open(fname, 'a')
writer = csv.DictWriter(outfile, delimiter = ';', lineterminator='\n', fieldnames=fieldnames)
## Add for each user the full name and email and for each group a 1 or 0, depending on group membership
for user in my_users:
    membership = []
        thisUser = {}
        thisUser['USERNAME'] = user.fullName
        thisUser['EMAIL'] =
        try: # Group membership outside the organisation will raise an error ("You do not have permissions to access this resource or perform this operation.")
            for group in user.groups:
        for title in my_group_titles:
            if title in membership:
                thisUser[title] = 1
                thisUser[title] = 0
        print("PLEASE NOTE: no information can be retrieved about user "+user.fullName+".")
print ("Ready: "'%c'))
print('The CSV file can be found here:')

And here is the provide_credentials script which is used above to login to AGOL:

import json, os
from getpass import getpass #to accept passwords in an interactive fashion

def provide_credentials():
    file_with_credentials = 'my_credentials.json'
    username = ''
    password = ''
    if os.path.exists(file_with_credentials):
        with open(file_with_credentials) as f:
            data = json.load(f)
            username = data['username']
            password = data['password']
    if not username or username == 'USERNAME' or not password or password == 'PASSWORD':
        username = input('Please enter your username: ')
        password = getpass('Please enter your password (this will remain invisible): ')
    return username, password

With the input file my_credentials.json:


Happy coding!


About the Author
GIS Consultant at Avineon-Tensing