How to export group members in Portal to CSV file

1754
6
Jump to solution
03-19-2020 09:21 AM
JoePrimicias
New Contributor II

I'm an owner in a Group in Portal. I cannot find a was to select all members and then export to a CSV file.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MehdiPira1
Esri Contributor

@JoePrimicias ,

The following python code extracts group members based on number of groups you want. (Here it's set to 10 groups). Just input the portal url, your credentials and the path to the output csv file.

from arcgis.gis import GIS
import pandas as pd

gis = GIS(portalURL, username, password)
group_members_list = []
groups = gis.groups.search('*', max_groups=10)
for group in groups:
    groupMembers = group.get_members()['users']
    dictionary = {'Group Name':group.title,'Group Members':groupMembers}
    group_members_list.append(dictionary)
df = pd.DataFrame(group_members_list)
df.to_csv(r'path to ...group_members.csv',index=False)

 

View solution in original post

6 Replies
Robert_LeClair
Esri Frequent Contributor

Hmmm...I know Administrators of the Portal can use a command line to list members of the Portal and export it out as a *.txt file using the ListUsers utility.  Not sure if it can do the same for only a Group.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hey Joe,

 

This can be accomplished using the ArcGIS API for Python.  Ex:

 

from arcgis.gis import GIS
import csv

portalURL = 'https://countysandbox.maps.arcgis.com'
username = 'jskinner_CountySandbox'
password = '******'
outputFile = r'C:\temp\output.csv'
groupName = 'Citizen Problem Reporter'

gis = GIS(portalURL, username, password)

group = gis.groups.search('title:{0} AND owner:{1}'.format(groupName, username), max_groups=15)[0]
groupMembers = group.get_members()['users']


with open(outputFile, 'w') as output:
    dataWriter = csv.writer(output, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
    dataWriter.writerow(['Username'])
    for user in groupMembers:
        dataWriter.writerow([user])
output.close()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
KevinPerry2
New Contributor II

Ok is this script calling out to the AGOL platform? What if we have an on-prem Enterprise Portal developed, and  I have many groups that I need to export users list? Will the above still work, I am getting the following error:

IndexError                                Traceback (most recent call last)
<ipython-input-1-222164fc7561> in <module>
     10 gis = GIS(portalURL, username, password)
     11 
---> 12 group = gis.groups.search('title:{0} AND owner:{1}'.format(groupName, username), max_groups=15)[0]
     13 groupMembers = group.get_members()['users']
     14 

IndexError: list index out of range
0 Kudos
MehdiPira1
Esri Contributor

@JoePrimicias ,

The following python code extracts group members based on number of groups you want. (Here it's set to 10 groups). Just input the portal url, your credentials and the path to the output csv file.

from arcgis.gis import GIS
import pandas as pd

gis = GIS(portalURL, username, password)
group_members_list = []
groups = gis.groups.search('*', max_groups=10)
for group in groups:
    groupMembers = group.get_members()['users']
    dictionary = {'Group Name':group.title,'Group Members':groupMembers}
    group_members_list.append(dictionary)
df = pd.DataFrame(group_members_list)
df.to_csv(r'path to ...group_members.csv',index=False)

 

View solution in original post

KevinPerry2
New Contributor II
Thank you for the response. I will try this, much appreciated.
0 Kudos
KevinPerry2
New Contributor II

That worked thank you very much. Now I just need to format it.

Curious. If I need to get the output for each groupMember's First and Last Name and email for each member of the group?

0 Kudos