Getting stuck adding users to groups with python

431
2
Jump to solution
01-25-2024 11:55 AM
chill_gis_dude
New Contributor III

Hey all,

I am trying to use a very simple python script to add users to a group and am getting stuck. It works successfully with one group but when I add more I get errors. Here is my code:

from arcgis import *
gis = GIS("url", "username", "password")
list1 = ['AdvancedUser', 'DefaultUser']
grouping = gis.groups.search('title: group1')
grouping[0].add_users(list1)

When I try with multiple groups here:

from arcgis import *
gis = GIS("url", "username", "password")
list1 = ['AdvancedUser', 'DefaultUser']
grouping = gis.groups.search('title: group1', 'title: group2')
grouping.add_users(list1)

I get the error:

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

Thank you for any help you can provide.

0 Kudos
1 Solution

Accepted Solutions
Raul
by
New Contributor III

The results of the search operation is a list or results. When you did:

grouping = gis.groups.search('title: group1')
grouping[0].add_users(list1)

You're using the add_users function of the first (index 0) object in the grouping list.

What you have to do, is then iterate over all the elements on the results list and use the add_users function on each of the objects of the list.

from arcgis import *
gis = GIS("url", "username", "password")
list1 = ['AdvancedUser', 'DefaultUser']
grouping = gis.groups.search('title: group1', 'title: group2')
for grp in grouping:
    grp.add_users(list1)

View solution in original post

2 Replies
Raul
by
New Contributor III

The results of the search operation is a list or results. When you did:

grouping = gis.groups.search('title: group1')
grouping[0].add_users(list1)

You're using the add_users function of the first (index 0) object in the grouping list.

What you have to do, is then iterate over all the elements on the results list and use the add_users function on each of the objects of the list.

from arcgis import *
gis = GIS("url", "username", "password")
list1 = ['AdvancedUser', 'DefaultUser']
grouping = gis.groups.search('title: group1', 'title: group2')
for grp in grouping:
    grp.add_users(list1)
chill_gis_dude
New Contributor III

Thank you Raul! I was seriously I was overthinking it. This really helps. 

0 Kudos