Unable to add list of users to groups

12-07-2017 03:06 AM
New Contributor II

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: 

I have also tried:

 for user in UserList: 


for user in UserList: 

And finally:

 for user in UserList: 

Would be very grateful if anyone know of a solution or what I am doing wrong.

Thank you!

0 Kudos
6 Replies
Occasional Contributor III

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

#confirm users were added by calling get_members method on group
New Contributor II

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.

0 Kudos
New Contributor III

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')

Traceback (most recent call last):
  File "<string>", line 5, in <module>
AttributeError: 'list' object has no attribute 'add_users'
0 Kudos
Esri Contributor

Katy Haddock‌ 

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>")

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: []}
New Contributor II

Thank you jyaist-esristaff‌ that is really helpful.

0 Kudos
Occasional Contributor III

Bear in mind that if the users are external to the organisation, you have to invite them as opposed to add them as they have to accept the invitation.

0 Kudos