Using ArcGIS API for python How do I add all users in an AGOL organization to a group?

3335
9
10-11-2017 01:33 PM
BenVanderford1
New Contributor II

I am attempting to add all the users in my org to a group. 

I have been able to select the group (in this case it is called 'Nearmap').

My difficulty is in trying to select all the users. What is the command for that? 

Also what is the command to then add all those users to the group 'Nearmap'?

0 Kudos
9 Replies
DanPatterson_Retired
MVP Emeritus

Do you have the API reference ???

And I assume you have seen the sample notebooks 

BenVanderford1
New Contributor II

I've been through both the API reference and sample notebooks but remain  stuck on the part to add the users to the group.  Thanks for the reply. 

0 Kudos
GlennLetham2
Occasional Contributor II

Hey Ben,have you ever tried admin tools? You can do many group management tasks like what you are asking. The free tools in the ArcGIS Marketplace should accomplish what you need done. 

0 Kudos
BenVanderford1
New Contributor II

I know the admin tools were a bit out of date. I was trying with the arcgis.gis module first. I may have to revert to the admin tools now. 

GlennLetham2
Occasional Contributor II

hmm, not sure about out of date, fYI, the last update was Feb 2017 and of note, another update is coming very shortly. Good luck with things

0 Kudos
BenVanderford1
New Contributor II

I was hoping to create a python script to add all the users to a group nightly. I do have the Admin Tools Pro but it won't let me automate the steps. 

On a side note I did discover how to get all the users in an organization: In this case I have a maximum number of accounts of 2000. Again thanks for any help offered. 

users = gis.users.search(query="*",max_users=1200)

0 Kudos
SethLewis1
Occasional Contributor III
from arcgis import *

#declare gis
gis = GIS('portal url', 'admin user', 'admin pass')

#return list of users in portal
user = gis.users.search('', '', max_users=2000)

#specify group for method
group = gis.groups.search('title: your group title', '')

#add user list to group with specified index value since gis.groups.search returns a list
group[0].add_users(user)

#confirm users were added by calling get_members method on group with index value
group[0].get_members()
GRSMGIS
New Contributor

sethlewistempe‌ very helpful.....instead of all users, how would you set "users" to be a dictionary of a subset of all users? Let's say I have a CSV of 100 users that I want added to this group....

0 Kudos
SethLewis1
Occasional Contributor III

GRSM_GIS

Assuming that the users have already been added to your portal/organization, the following should get you started.

#import modules
from arcgis import *
import csv
                 
#declare gis
gis = GIS('portal url', 'admin', 'pass')

#specify group for method
group = gis.groups.search('title: group title', '')

#print list of members
group[0].get_members()

#declare empty list of users to be added
userAdd = []

with open(r"path to csv") as f_input:
    csv_input = csv.DictReader(f_input)
    
    for row in csv_input:
        users = row["provide column containing usernames"]
        
        userAdd.append(users)
        
        #print list result
        print(userAdd)
            
#pass the appended list to the method
group[0].add_users(userAdd)
        
#confirm users were added by again calling the get_members method on the group
group[0].get_members()
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍