How to export group members in Portal to CSV file

5047
8
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

8 Replies
Robert_LeClair
Esri Notable 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)

 

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
HillarySherrill
New Contributor II

We're you able to get the output to list names and emails?  I'm needing to do the same so would be curious to know your method if you were able.  Thanks!

0 Kudos
Kalu_Ribush
New Contributor II

How would I manipulate this code, so that the output CSV has a line for each user, with a column with the group names concatenated in it (rather than the other way around)?

Thanks

 

0 Kudos