Select to view content in your preferred language

Get users and Their Role and UserType

2637
8
12-20-2022 01:20 PM
kapalczynski
Regular Contributor

I am trying to query for all my users and get their email, Role and UserType

 

item.usertype does not exist

item.role returns something that is not what I am expecting

Role
org_user
org_user
org_user
org_user
org_admin

org_user

 

 

I am expecting the name of my Roles as seen below... they are custom roles and I need that to eventually set them up..

kapalczynski_0-1671571165350.png

 

How do I get the UserName, UserType, UserRole, UserEmail???

 

user_list = gis.users.search(query=search_user, max_users=200)

with open(output_csv, 'w', encoding='utf-8') as file:
    csvfile = csv.writer(file, delimiter=',', lineterminator='\n')
    csvfile.writerow(["Name",                         # these are the headers; modify according to whatever properties you want in your report
                      "Role",
                      "UserType",
                      "Email"
                    ])

    for item in user_list: 
        csvfile.writerow([item.username,          # modify according to whatever properties you want in your report
                          item.role,
                          item.usertype,
                          item.email
                          ])

 

 

0 Kudos
8 Replies
kapalczynski
Regular Contributor

 I do this I get this... 

I need the actual text that I am going to provide another script that will create the users.

So I need the Actual User Type of Creator, or Mobile Worker

So I need the Actual Role of  Administrator, VDOT_User, VDOT_Publisher of which they are assigned.

 

the result of 'both' does not tell me anything

    for item in user_list: 
        csvfile.writerow([item.username,          # modify according to whatever properties you want in your report
                          item.role,
                          item.userType,
                          item.email
                          ])

 

kapalczynski_0-1671571465103.png

 

 

0 Kudos
kapalczynski
Regular Contributor

I can do this and it actually gives me the correct names.... with the 4th one a custom role

What am I missing.. are they stored differently?   

If I was to write and create a user I am assuming I would have to provide one of these role names... But I cannot get them when I am querying for the current users.

 

<Role name: Viewer, description: Viewer>
<Role name: Data Editor, description: Data Editor>
<Role name: Facilitator, description: Facilitator>
<Role name: Data Editor_VDOT, description: This type of account is meant for data editors.>

 

rolesList = gis.users.roles.all(max_roles=50)
for role in rolesList:
print(role)

 

0 Kudos
JoshuaSharp-Heward
Occasional Contributor III

When I print out a list of user roles using user.role it only shows org_user/org_admin where the default "User" and "Admin" roles are being used, and for all of our custom roles it is printing the name correctly. Are you running this from a notebook in ArcGIS Online? And I'm assuming you've gone through the full list of user roles that have been output in your csv and confirmed that there's nothing other than org_user/org_admin/org_viewer?

0 Kudos
kapalczynski
Regular Contributor

I am running this from a simple .py file that is being launched by a .bat file.  We do not use any default roles.  Everything is a custom role. The python file is doing much more than this, but this is part of it

I need a list of the UserName, LastName, Email, userType, Role as seen in the image below.

AND A LIST that I could be able to user to come back and create, modify or delete these users in AGOL.

  1. John, Smith, john.smith@somerthing.com, (Creator/Field Worker), (custom Role name)
  • The problem is I can get a proper listing of Roles and Custom Roles with gis.users.roles.all but that is just a list of available user Roles.  I need them listed for each user.
  • If I search for users I can only get meaningless data with gis.users.search(query=search_user, max_users=2000)
    • This is all I can seem to get by using these:
    • item.username, item.role, item.roleId, item.userLicenseTypeId, item.level, item.email
    • ex.  users name, org_user/org_admin, 4uTOULA5tZCrtkZs, creatorUT/fieldWorkerUT, 11 or 2, email

org_user or org_admin does not tell me the users Role name which is a custom name.  Getting a list from gis.users.roles.all is worthless because it’s just a list of available Roles and NOT user specific.

 

It appears that what is being stored in AGOL User store is NOT what I am seeing in my original post. 

  • I see Creator or Mobile Worker but when I query for this data I get CreatorUT or FIeldWorkerUT
  • I see my my Custom Roles in the dropdown but when I query I get org_user or org_admin.

kapalczynski_0-1671713382569.png

 

0 Kudos
kapalczynski
Regular Contributor

It appears I can query the RoleId... This example should how to retrieve the description of the RoleId but NOT the Role ID itself...uggggg

What is the attribute of the Role Object that will give me the actual name

 

 

searched_role = gis.users.roles.get_role(item.roleId)

 

 

roleName = searched_role.description
print(roleName)

 

 

RESULT

"This type of account is meant for data editors, including mobile/field data collectors. This account cannot create content or groups, but can participate in groups, view VDOT content, and edit data shared with them."

BUT 

how do I get the Role Name as you can see below <Role name: role1, description: role1>,

I tried:

roleName = searched_role.RoleName

roleName = searched_role.roleName

roleName = searched_role.Role Name

 

 

Querying the roleId property of a User returns you the custom roles' ID. You can use this to search for that role to know more details or create another user with the same role:

tiles_pub_user.roleId
'rYzfnni7g5AvFsRz'
searched_role = gis.users.roles.get_role(tiles_pub_user.roleId)
searched_role.description
'User that can publish tile layers'

 

Listing all the custom roles in an org

When migrating users from one org to another or even to duplicate an org on new infrastructure, you would go through the process of cloning the users and their roles. For this, you can get the list of roles using the all() method on the RolesManager resource object:

gis.users.roles.all(max_roles=50)
[<Role name: Viewer, description: Viewer>,
 <Role name: tiles_publisher, description: User that can publish tile layers>,
 <Role name: role1, description: role1>,
 <Role name: role1, description: role1>,
 <Role name: role1, description: role1>,
 <Role name: role1, description: role1>]

0 Kudos
JoshuaSharp-Heward
Occasional Contributor III

I was going to suggest accessing it via the roleId, if we can get that to work! It should just be 

searched_role.name

to return the role name once you have the role object!

0 Kudos
ZandBakhtiari1
New Contributor III

Did you ever figure this out?  I'm hitting the same wall you were in this post.

0 Kudos
EnvironmentalIntelligence
New Contributor III

Seeing the same issue, but with some success using csv writer to write the role names.  Testing with f strings in Python window returns the actual role name for me.

portal_users = gis.users.search(max_users=1000)

for user in portal_users:
print(f"{user.username} - {user.role")

 

 

0 Kudos