Select to view content in your preferred language

Python Query Users by Category

1883
8
Jump to solution
02-13-2023 02:29 PM
by Anonymous User
Not applicable

Hello, I have 2 questions about accessing users and their assigned categories in ArcGIS Online through python:

 

1. I'm trying to query all the users who haven't been assigned a category. This is what I first tried:

 

users = gis.users.search(query='categories:""', max_users=1000)

 

It returned users who weren't categorized as well as 2 full user categories. Is this the incorrect method of searching for unassigned users? I also tried searching with query='None' but the result was an empty list.

The next thing I tried was using NOT in my filters. My logic was to use search(NOT (Category A OR Category B OR Category C)), in order to find the users without a category. I was trying to follow the California NOT Imagery examples here https://doc.arcgis.com/en/arcgis-online/reference/advanced-search.htm but am getting a Syntax Error: invalid syntax.

 

users = gis.users.search(query='categories:""' NOT 'categories:"/Categories/Pets"', max_users=1000)

 

Could I ask how to search for users without a category assigned to them?

 

2. Is there a way to assign users a category through python? I couldn't find any documentation on how to do this through the API.

 

Thank you!

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

In AGOL, this is a built-in feature:

jcarlson_0-1676384720985.png

And if you watch the URL parameters when you click this, you can see that the supplied query is

...&categories=["_none_"]

But oddly, if you are using the search query string, it's the literal word "null". Maybe just a difference in how the Python API and the REST endpoint interpret things?

jcarlson_1-1676384980574.png

Anyway, the point is you can query for non-categorized members. And according to the API docs, you can assign categories easily enough.

assign_categories(users, categories)

Adds categories to users.

Parameter

Description

users

Required list of User objects to categorize.

categories

Required string defining the categories to add to each user in the users argument list.

 

- Josh Carlson
Kendall County GIS

View solution in original post

0 Kudos
8 Replies
DavidPike
MVP Frequent Contributor

Not sure but would it work to return all users then NOT IN compare against the category list?

0 Kudos
by Anonymous User
Not applicable

I was trying that on a smaller scale and my syntax works now but the query is still returning all the users, regardless of category. This is the boolean I'm using:

users = gis.users.search('categories:"" NOT "/Categories/Pets"', max_users=1000)

 

0 Kudos
jcarlson
MVP Esteemed Contributor

In AGOL, this is a built-in feature:

jcarlson_0-1676384720985.png

And if you watch the URL parameters when you click this, you can see that the supplied query is

...&categories=["_none_"]

But oddly, if you are using the search query string, it's the literal word "null". Maybe just a difference in how the Python API and the REST endpoint interpret things?

jcarlson_1-1676384980574.png

Anyway, the point is you can query for non-categorized members. And according to the API docs, you can assign categories easily enough.

assign_categories(users, categories)

Adds categories to users.

Parameter

Description

users

Required list of User objects to categorize.

categories

Required string defining the categories to add to each user in the users argument list.

 

- Josh Carlson
Kendall County GIS
0 Kudos
by Anonymous User
Not applicable

Hi Josh, the null search query worked perfectly. Thank you so much! For the assign_categories command, I'm getting an Unresolved Reference 'assign_categories' error. When I run the code, there's a NameError: name 'assign_categories' is not defined. I checked out the documentation you sent and used it with the user and category parameters. Any ideas?

0 Kudos
jcarlson
MVP Esteemed Contributor

Are you running your code in AGOL, or locally? You may want to check what version of the arcgis module is currently installed in the env you're using.

- Josh Carlson
Kendall County GIS
0 Kudos
by Anonymous User
Not applicable

I'm running it while establishing a connection and logging into AGOL. I ran the command arcgis.__version__ before connecting and my version is 1.9.0 but the release notes has the method in 2.1.0, so I'll look into updating mine.

A follow-up question:

Victoria_0-1676390331270.png

The assign_categories documentation is under the UserManager class. When accessing something from this class, do you need a line manager = arcgis.gis.UserManager to then access methods as manager.assign_categories(), for example?

0 Kudos
jcarlson
MVP Esteemed Contributor

Per the docs:

This class is not created by users directly. An instance of this class, called ‘users’, is available as a property of the Gis object. Users call methods on this ‘users’ object to manipulate (create, get, search, etc) users.

So just call gis.users.assign_categories, and pipe in your list of uncategorized folks!

- Josh Carlson
Kendall County GIS
by Anonymous User
Not applicable

Hi Josh, I found this in the same docs section, right below assign_categories and thought I'd try this first since the .categories command works on my currently installed version.

Victoria_0-1676413549309.png

I'm trying to follow the usage example 1. Right now, my code looks like this:

# testing on 1 user first: 
uncategorized_users = gis.users.search('categories:null', max_users=1)
user_categories[0] = ["/Categories/Pets"]

for user in uncategorized_users:
    list_index = 0
    user.categories = user_categories[list_index]
    ++list_index

The code runs without any errors but the user isn't assigned the new category. The docs says .categories can be used for getting/setting so I'm not sure what's wrong...

0 Kudos