Hi, I am trying to add a list of users to a group using the Python API but can not get it to work correctly. I have tested printing out the list and that works but it wont add them to a group.
This is what I have tried:
for user in UserList:
TheGroup.add_users(user)
I have also tried:
for user in UserList:
TheGroup.add_users(user.username)
Also:
for user in UserList:
TheGroup.add_users(user['username'])
And finally:
for user in UserList:
TheGroup.add_user([user])
Would be very grateful if anyone know of a solution or what I am doing wrong.
Thank you!
Instead of looping through your list, try passing the list directly to the add_users method.
#import modules
from arcgis import *
#declare gis
gis = GIS('portal URL', 'admin user', 'password')
#declare list of users
user = ['user1', 'user2', 'user3']
#specify group for method
group = gis.groups.search('title: Group title', '')
#add user list to group with index value declared since group search returns list
group[0].add_users(user)
#confirm users were added by calling get_members method on group
group[0].get_members()
Thank you Seth. Unfortunately this method will not work for me as the list of users I am creating is generated based on role type and the users in the account are changing daily so I need to automate the script to run on a daily basis.
If you have any other ideas that would be great.
Thank you.
Hey Seth,
You wouldn't know how to use this code to add users to multiple groups? When I try I get the following error:
gis = GIS("link", "admin user", "password")
list1 = ['AdvancedUser', 'DefaultUser']
group = gis.groups.search('title: group1', 'title: group2')
group.add_users(list1)
Traceback (most recent call last):
File "<string>", line 5, in <module>
AttributeError: 'list' object has no attribute 'add_users'
As Seth Lewis points out, the Group item's add_users method requires a Python string that is the name of a list, not a string that represents the name of a user. For the instance you describe, it sounds like you can query the gis object representing your GIS, then create a list based on the role. For instance, you could create a list of publishers and add them to a group:
my_gis = GIS("portal url", "user", "password")
portal_users = my_gis.search.users()
list_of_users = [a_user for a_user in portal_users if a_user.role == 'org_publisher']
my_group = my_gis.groups.search("title: <title of your group>")
my_group.add_users(list_of_users)
When successful, the add_users method returns a dictionary with a notAdded key with a list of users that were not added - so an empty list is the ideal value...(Less is definitely more 😞
{notAdded: []}
Thank you jyaist-esristaff that is really helpful.