Query all groups that a member belongs to

511
2
Jump to solution
02-12-2021 06:55 AM
BobCarberry1
New Contributor II

I realize that you can query all groups a particular user is an owner of, like this: groups = gis.groups.search(query='owner:Portal_Admin') , but I want to query all groups that  a particular user is a member of.  Like this:  groups = gis.groups.search(query='member:Portal_Admin').  Is this possible?

0 Kudos
1 Solution

Accepted Solutions
MinbinJiang
Esri Contributor
2 Replies
DavidPike
MVP Frequent Contributor

I've got a simple script which should list all portal users in an output csv, with a column containing a list of all their group memberships).  It might be helpful.

from arcgis.gis import GIS
import pandas as pd
import datetime


url = 'https://yourportal/portal/'
username = ''
password = ''
out_csv = r'C:\portal_group_membership.csv'

gis = GIS(url, username, password)

user_list = gis.users.search(max_users=10000)

user_group = []
for user in user_list:
    group_list = []
    for group in user.groups:
        group_list.append(group.title)
    

        
    created_time = datetime.datetime.fromtimestamp(user.created/1000).strftime('%Y-%m-%d %H:%M:%S')
    
    try:
        last_login_time = datetime.datetime.fromtimestamp(user.lastLogin/1000).strftime('%Y-%m-%d %H:%M:%S')
    except:
        last_login_time = -1
        

    
    user_group.append([user.username, user.fullName, user.email, user.level, created_time, last_login_time, (group_list)])
    
user_group_df = pd.DataFrame(user_group, columns =['User', 'Full_Name', 'Email', 'Level', 'Created_Date', 'Last_Login', 'Groups'])
user_group_df.to_csv(out_csv)
MinbinJiang
Esri Contributor

get_groups_user_belongs_LI.jpg