Select to view content in your preferred language

Get Users and Other info from AGOL User Store

1590
9
Jump to solution
02-23-2023 05:12 AM
kapalczynski
Frequent Contributor

I am trying to write a script that will get me the users, email, user name, User Type and Role

I can do the below but cant find the type for User Type and Role

 

Any thoughts on how to get their User Type and Role?

 

 

 

 

 

from arcgis.gis import GIS
import arcgis 
import pandas as pd



agol_username = 'xyz' 
agol_password = '\xyz'  
output_csv = r'C:\Users\Desktop\projects\AGOL_Python\users.csv'

search_user = '*'
gis = GIS('https://xyz.maps.arcgis.com', agol_username, agol_password)
user_list = gis.users.search(query=search_user, max_users=2000)

output = []
for item in user_list:

    email = item.email
    username = item.username
    z = [email] + (username if isinstance(username, list) else [username])
    print(z)
    output.append(z)
print("")
print(output)


df = pd.DataFrame(output,columns=['name','email'], dtype=float)
print(df)

 

 

 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
Kepa
by Esri Contributor
Esri Contributor

Digging into the API reference you can get the custom role ids this way:

 

roles = gis.users.roles.all()

 

Create a dictionary with role ids, which gives us ID:roleName key-value pairs:

 

rol_ids = {role.role_id: role.name for role in roles}

 

Finally, use the roleId property of user's object to get the name like this (I used an exception to catch default roles of the organization):

 

for user in users:
    try:
        print(user.username, rol_ids[user.roleId])
    except KeyError as e:
        print(user.username, e)

 

 

 Hope that helps!

View solution in original post

9 Replies
Kepa
by Esri Contributor
Esri Contributor

You are in the right way, you just need to go further on the user object properties:

 

# Level 1 = Viewer , 2 = Creator, not sure about Editor & Mobile worker levels
item.level
item.role
item.userLicenseTypeId

 

Python API Reference (User) 

0 Kudos
kapalczynski
Frequent Contributor

Thanks

I get this return ... where do I get a list of values for LEVEL?  

RETURN:

['gg.ggg@xyz.xyz', 'xy.xyz@xyz.xyz', '11', 'org_user', 'fieldWorkerUT']

 

How do I define 11?

What is org_user?

0 Kudos
kapalczynski
Frequent Contributor

Fir instance this is one return

 

['xyz.xyz@xyz.gov', 'xyz.xyz@xyz.gov', '2', 'org_admin', 'creatorUT']

2 = Creator?

org_admin is not my role I see when I look this person up in AGOL

 

 

This person is a Creator and has a Custom Role of Publisher+

What is 'creatorUT'?

 

kapalczynski_0-1677162696617.png

 

0 Kudos
Kepa
by Esri Contributor
Esri Contributor

There are three possible values under role property:

org_userorg_publisher and org_admin

The only explanation for what you get is that custom roles are named as 'org_admin'. 

Edit: 

Snipaste_2023-02-23_16-16-49.png

0 Kudos
kapalczynski
Frequent Contributor

 

Is there a way to query the user and get back their Role that is assigned via AGOL.

I query the users and get values that are not the same values as when you add a new Member.  This is VERY confusing.  If I am trying to add a user programmatically I see Mobile Worker in AGOL but:

  • when I query via python I see fieldWorkerUT. 
  • They are a creator and when I query I get 2

I need to be able to bridge these terminologies so I can query and modify users programmatically.

For example when I query this person I get this:

         ['xyzx@xyzx.gov', 'xyzx@xyzx.gov', '11', 'org_user', 'fieldWorkerUT']

BUT when I look at this users in AGOL I get this:

         MobileWorker and Data_Editor (which is a custom Role)

kapalczynski_1-1677166126928.png

 

  1. I assume that fieldWorkerUT and Mobile Worker are the same thing?
  2. How to I query for the custom role that is set for this user?
  3. If I want to add a user programmitically do I say fieldWorkerUT or MobileWorker
  4. How would I refer to the custom role (Data_Editor vs org_user)

 

 

0 Kudos
Kepa
by Esri Contributor
Esri Contributor

1. Yeah, I think so, but AGOL is always evolving so the exact name could change in the future

2. Answered below

3. and 4. When calling create method when adding users you can assign a custom role passing the specific role ID in the role parameter.

0 Kudos
kapalczynski
Frequent Contributor

If I use this admin tool I can read all the users

And it reads it correct... When Cant I do that via python

 

kapalczynski_1-1677167955765.png

 

 

 

0 Kudos
Kepa
by Esri Contributor
Esri Contributor

Digging into the API reference you can get the custom role ids this way:

 

roles = gis.users.roles.all()

 

Create a dictionary with role ids, which gives us ID:roleName key-value pairs:

 

rol_ids = {role.role_id: role.name for role in roles}

 

Finally, use the roleId property of user's object to get the name like this (I used an exception to catch default roles of the organization):

 

for user in users:
    try:
        print(user.username, rol_ids[user.roleId])
    except KeyError as e:
        print(user.username, e)

 

 

 Hope that helps!

kapalczynski
Frequent Contributor

@Kepa  Thanks so much this is exactly what I was looking for.... I posted my script below incase anyone wants to reference....

I am writing this to a CSV file as well as displaying in a data frame 

 

 

 

from arcgis.gis import GIS
import arcgis 
import pandas as pd
import csv

agol_username = 'xyzxyzx' 
agol_password = 'xyzxyzx'  
output_csv = r'C:\Users\Desktop\GIS_projects\AGOL_Python\users.csv'
search_user = '*'
gis = GIS('https://xyzx.maps.arcgis.com', agol_username, agol_password)
user_list = gis.users.search(query=search_user, max_users=5)
roles = gis.users.roles.all()
rol_ids = {role.role_id: role.name for role in roles}
output = []
counter = 0

with open(output_csv, 'w', encoding='utf-8') as file:
     csvfile = csv.writer(file, delimiter=',', lineterminator='\n')
     csvfile.writerow(["username",                         # these are the headers; modify according to whatever properties you want in your report
                       "email",
                       "roleID",
                       "level",
                       "disabled",
                       "role",
                       "licenseType"
                     ])
     for user in user_list:
        try:
            varusername = user.username  
            varemail = user.email
            varRoleID = rol_ids[user.roleId]
            varlevel = user.level
            vardisabledValue = user.disabled
            varrole = user.role
            varlicenceType = user.userLicenseTypeId

            z = (varusername if isinstance(varusername, list) else [varusername]) + [varemail] + [varRoleID] + [varlevel] + [vardisabledValue] +  [varrole] + [varlicenceType]
            output.append(z)

            counter += 1
            csvfile.writerow([varusername,  
                        varemail,
                        varRoleID,
                        varlevel,
                        vardisabledValue,
                        varrole,
                        varlicenceType 
                        ])
        except KeyError as e:
            print(user.username, e)

df = pd.DataFrame(output,columns=['name','email','RoleID','Level','disabled','role','licensetype'], dtype=float)
print(df)

 

 

 

0 Kudos