Select to view content in your preferred language

Unable to get list of users in a role

3296
4
12-11-2017 07:52 AM
MikeQuetel
Deactivated User

Our portal installation is configured with enterprise logins and allows users to auto create an account when they authenticate for the first time.  We also have a custom role called "Auto_Created" which is the default role for new user accounts.  I would like to get a list of users in this role and perform some nightly administrative tasks. I can get the role via the roleManager, but I'm unable to successfully perform a search for users with the role name as criteria (see attached screen grab). 

[EDIT] Yes, I am certain that more than 0 users in our portal are in the  "Auto_Created" role. 

Would appreciate any pointers.  Thanks!

4 Replies
SethLewis1
Frequent Contributor

Add an empty parameter first in your search method. See below for pseudo code.

I tested your screenshot code against our portal by passing 'role: custom_role' as the only parameter and returned 0 users. When I add an empty string parameter, it returns the proper count.

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

#return list of users in portal
users = gis.users.search('', 'role: Auto_Created')
print("Users returned: " + str(len(users)))
0 Kudos
MikeQuetel
Deactivated User

Thank you for taking the time to reply.  Unfortunately your example code didn't work for me, It's still not returning any users from that custom role.  I think the issue is that the role name may not be valid criteria for use in the query. For example, in searching for users in the 'Administrator' role nothing would be returned. If, however, I use 'org_admin', which I found buried in some documentation it works. This leads me to wonder what I'm supposed to use as criteria for searching by custom role and where I'm supposed to find it?

Docs concerning roles:

http://doc.arcgis.com/en/arcgis-online/reference/roles.htm

Docs were I found the 3 constants (org_user, org_publisher, org_admin) for built-in roles:

https://developers.arcgis.com/python/guide/accessing-and-managing-users/#about-user-roles

Example:

users = gis.users.search('role:Administrator')     # returns 0

users = gis.users.search('role:org_admin')         # returns actual count   

JohnYaist1
Esri Contributor

Hi Mike Quetel‌ - I haven't figured a way to make a list of users in a role directly from a gis.users.search(), but I generated one using role_id values in this way. The users list in the second block of code returned the correct users in the portal I tested.  Will that work to get your list?

g = GIS("portal url", "username", "password")

role_mgr = arcgis.gis.RoleManager(g)
roles = role_mgr.all()
for role in roles:
 print("Role ID: {}".format(role.role_id))

users = [user for user in g.users.search() if user.role == '<role.role_id value>']
for user in users:
 print("User: {:<20}\tRole: {}".format(user.username, user.role))‍‍‍‍‍‍‍‍‍
WillHughes
Occasional Contributor

This also works. For some reason I was unable to return all users when using the code in line 1.

users = gis.users.search(query='role: <enter role id here>', sort_field='created', sort_order='asc', max_users=200, outside_org=False)‍
0 Kudos