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)
Solved! Go to Solution.
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!
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
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?
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'?
There are three possible values under role property:
org_user, org_publisher and org_admin
The only explanation for what you get is that custom roles are named as 'org_admin'.
Edit:
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:
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)
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.
If I use this admin tool I can read all the users
And it reads it correct... When Cant I do that via python
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!
@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)