Access and manage HUB user accounts

745
6
Jump to solution
10-05-2022 02:07 PM
JaredPilbeam2
MVP Regular Contributor

I'm attempting to access and manage users in our HUB community account with the python API. I'm using our only sign-in credentials.

 

gis = GIS("https://countyhub.maps.arcgis.com", "username", "password")

 

To get the number of members I use:

 

hub_users = gis.users.search()
number = len(hub_users)
number

#prints
100

 

I don't know where it gets the 100? Our Members page says we have 538 enabled members with any sort of role.

JaredPilbeam2_0-1665001902775.png

Then I attempt to gather some info with the search queries and I'm blocked by an Exception.

 

# anonymous connection to ArcGIS Online
ago_gis = GIS()
# # search the users whose email address ends with esri.com
esri_public_accounts = ago_gis.users.search(query='email = @esri.com')
len(esri_public_accounts)

 

Exception: You do not have permissions to access this resource or perform this operation. (Error Code: 403)

 

Additionally, I've tried with my AGOL sign-in credentials. It also throws the same Exception with querying.

0 Kudos
1 Solution

Accepted Solutions
JaredPilbeam2
MVP Regular Contributor

Found out you have to utilize the UserManager class (with admin privileges).

This will get you a list which includes the count of users in your org:

from itertools import count
import arcgis

users = arcgis.gis.UserManager(gis)

totalUsers = users.counts(type='user_type', as_df=False)
totalUsers
#prints
[{'key': 'creatorUT', 'count': 538}]

#index it to pull out the integer
totalUsers = users.counts(type='user_type', as_df=False)[0]['count']
totalUsers
#prints
538

 

Credit goes to this SE answer. This answer has another useful line of code. It allows you to print out the names in a list.

allUsers = users.search(query=None, max_users=totalUsers)
allUsers

View solution in original post

6 Replies
RhettZufelt
MVP Frequent Contributor

Don't know if there is some kind of limit that it will return as I only have 92 members in my Org.

RhettZufelt_0-1665013082914.png

Which is exactly what your code reports:

RhettZufelt_1-1665013121218.png

 

R_

JaredPilbeam2
MVP Regular Contributor

I was thinking that may be the case. I'm not sure how to avoid that limit either. I put it in an if else statement, and it still was counting 100 or less. But I'm not sure how much you can trust that.

hub_users = gis.users.search()
number = len(hub_users)
if number >= 100:
    print('members exceed 100')
else:
    print('members are 100 or less')

#prints
members are 100 or less
0 Kudos
RhettZufelt
MVP Frequent Contributor

This was bothering me so I added some users and tested again.

For whatever reason, it does appear that the results are limited to 100 users:

RhettZufelt_0-1665067968188.pngRhettZufelt_1-1665067985479.png

R_

 

 

 

 

 

JaredPilbeam2
MVP Regular Contributor

Interesting.

I may have made a mistake on my last test with the if else statement. I think I was signed in to my AGOL account. Just now, I made sure I was using Hub credentials and I get this:

hub_users = gis.users.search()
number = len(hub_users)
if number >= 100:
    print('members exceed 100')
else:
    print('members are 100 or less')

#prints
members exceed 100

 

Where to take it from here?

0 Kudos
RhettZufelt
MVP Frequent Contributor

Next I would change the >= to just > and see what it says (or look at value of number).

The code as is will say it exceed 100 even if it is equal to 100, so can't tell if you are still getting the same result of 100.

R_

0 Kudos
JaredPilbeam2
MVP Regular Contributor

Found out you have to utilize the UserManager class (with admin privileges).

This will get you a list which includes the count of users in your org:

from itertools import count
import arcgis

users = arcgis.gis.UserManager(gis)

totalUsers = users.counts(type='user_type', as_df=False)
totalUsers
#prints
[{'key': 'creatorUT', 'count': 538}]

#index it to pull out the integer
totalUsers = users.counts(type='user_type', as_df=False)[0]['count']
totalUsers
#prints
538

 

Credit goes to this SE answer. This answer has another useful line of code. It allows you to print out the names in a list.

allUsers = users.search(query=None, max_users=totalUsers)
allUsers