List of All Portal Users

2048
3
Jump to solution
02-26-2020 08:48 AM
JosephRoberts2
New Contributor

I need to delete all the Users from a particular Group on my Portal.  I initially attempted a group first approach but realized that there wasn't an efficient way to access the users via the group.  Therefore, I took a user approach. 

The logic for identifying the users group and deleting the users is working however, I have yet to find a way to iterate through all of the portal users.  The search either times out or returns the errors below.

I know it has something to do with the query but I have yet to find adequate search query documentation for the Python API.

Search—ArcGIS REST API: Users, groups, and content | ArcGIS for Developers 

Search reference—ArcGIS REST API: Users, groups, and content | ArcGIS for Developers 

Can someone please point me in the direction of the correct user search syntax or assist with troubleshooting this issue?

Thanks. 

0 Kudos
1 Solution

Accepted Solutions
BenTurrell
Occasional Contributor III

Hey Joseph Roberts‌,

Have you seen this document: Accessing and managing groups | ArcGIS for Developers 

It has a get_members function that will give you a list of users. From there you can delete the users.

Thanks,

Ben


If this answer has helpful please mark it as helpful. If this answer solved your question please mark it as the answer to help others who have the same question.

View solution in original post

3 Replies
BenTurrell
Occasional Contributor III

Hey Joseph Roberts‌,

Have you seen this document: Accessing and managing groups | ArcGIS for Developers 

It has a get_members function that will give you a list of users. From there you can delete the users.

Thanks,

Ben


If this answer has helpful please mark it as helpful. If this answer solved your question please mark it as the answer to help others who have the same question.

WilliamCraft
MVP Regular Contributor

Here is some code to iterate through users within Portal:

source = GIS("https://servername.domain.com/portal", "username", "password", verify_cert=False)
source_users = source.users.search('!esri_ & !portaladmin')
for user in source_users:
       print(user.username)‍‍‍‍‍‍
LaurensGIS
New Contributor III

This solution only gets you the first 100 users. I used advanced_search to get more users, but I believe there is still a maximum. If you reach the maximum you have to use paging. 

But how did you figure out what the right search query was? 

source = GIS("https://servername.domain.com/portal", "username", "password", verify_cert=False)
source_users = source.users.advanced_search(query='!esri_ & !portaladmin', max_users = -1)
for user in source_users.get('results'):
    print(user.username)

 

This is my solution with paging. It works, but I'm not an Python expert, so I'm not sure this is the best method. It searches for 100 users at a time. When there are less than 100 users in the result, it means it has reached the last page. 

source = GIS("https://servername.domain.com/portal", "username", "password", verify_cert=False)
start = 0
lenght_searchresult = 100
while lenght_searchresult == 100:
    source_users = source.users.advanced_search(query='!esri_ & !portaladmin', start = start, max_users = 100)
    list_users = source_users.get('results')
    lenght_searchresult = len(list_users)
    start = start + 100
    for user in list_users:
        print(user.username)