Select to view content in your preferred language

Unable to add list of users to groups

2564
5
12-07-2017 03:06 AM
KatyHaddock
Occasional Contributor

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!

0 Kudos
5 Replies
SethLewis1
Frequent Contributor

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()
KatyHaddock
Occasional Contributor

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
JohnYaist1
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>")
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: []}
KatyHaddock
Occasional Contributor

Thank you jyaist-esristaff‌ that is really helpful.

0 Kudos
MichaelKelly
Frequent Contributor

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