Categorize items with the API

3164
9
08-24-2018 04:23 AM
MortenBjerrum1
Esri Contributor

Is there a way to categorize items through the API (to group categories)? I would like to set this as a property just like title, tags etc. Is this not yet implemented in the API for Python?

Tags (1)
0 Kudos
9 Replies
simoxu
by MVP Regular Contributor
MVP Regular Contributor

I tested the following code in Python API 1.5.0, it works.

from arcgis.gis import GIS
gis=GIS(url="https://orgname.maps.arcgis.com",username="abc")

# get the CategoryManager for this GIS
cs=gis.admin.category_schema

# get the item you want to work on
item1=gis.content.get("<item ID>")

# assign the item to the desired categories
# you may need to create the categories first in AGOL 
cs.categorize_item(item1,['/Categories/People', '/Categories/Boundaries'])

MortenBjerrum1
Esri Contributor

Thank you for the answer. It looks like the Category_schema is the schema for the entire GIS organisation. In this case I am using Group categories. How to access them?

0 Kudos
simoxu
by MVP Regular Contributor
MVP Regular Contributor

I am not sure I understand the question. what do you mean "Group categories"? is it "Sub Categories"? If yes, I guess you can access sub categories the same way using the code provided. CategoryManager is where all the relevant functions are implemented.

CategoryManager — arcgis 1.5.0 documentation 

0 Kudos
MortenBjerrum1
Esri Contributor

Sorry if my question is unclear. There seems to be categories both at the organisation level and at group level - https://doc.arcgis.com/en/arcgis-online/share-maps/own-groups.htm. Your script access the category_schema at the organisation level (from the gis object) but can I access a similar schema from the group level? Right now, I manually categorized all items but would still like to be able to do it with the API...

0 Kudos
simoxu
by MVP Regular Contributor
MVP Regular Contributor

uhm...

Unfortunately I can't find any implementation of the group category in Python API. So it seems you have to use REST API to do it.

To list the the group content categories :

Group Category Schema—ArcGIS REST API: Users, groups, and content | ArcGIS for Developers 

To update the group content categories :

Update Group Items with Content Categories—ArcGIS REST API: Users, groups, and content | ArcGIS for ... 

Google "how to send http request using python" will get the coding part sorted for you.

Good luck!

0 Kudos
MortenBjerrum1
Esri Contributor

Thank you. I will try that way.

0 Kudos
JimBarry
Esri Regular Contributor

This can be done with v1.7.1 now.

The Group class has a categories property, that gives you access to the CategorySchemaManager for that group. 

Then you can use the .assign_to_items() method to assign group categories to items that belong to the group.

ZacharyUhlmann1
Occasional Contributor III

Thanks @JimBarry this worked for me.  I included a general example of what I did in case anybody is interested.  The documentation for the assign_to_items() method shows the function call with a JSON array of item objects - 3 in that example.  My array is an array of 1, but obviously more efficient to update or assign categories to multiple items at once.  I will pursue this from a Pandas Dataframe.  I will update or post again once I get to that point.  BOTTOM LINE:  assigning categories to groups is very doable.  FYI - a nice article on the Category Gallery Instant App posted by ESRI's own @BernSzukalski  that inspired me to create Group Categories for organizational purposes specifically for the Category Gallery.

from arcgis.gis import GIS
from arcgis.gis import Group
my_gis = GIS(username = u_name, password = p_word)
# group_id is a string of numbers of text FYI (for those unaware)
group_id = <find itemid for group, i.e. dc74b222222269e9a54f81afaed0066>
# get group
target_group = Group(my_gis, group_id)
# search items (adjust this to find your items)
items = target_group.content.search(query = "owner: <my_agol_username> AND group:{}".format(group_id),item_type="Feature Service", max_items = 125)
# index of 0 is an an example.  Find index for target_item
item = items[0]
# you need item_id # as with group_id above
item_id = item.item_id
# category = transportation in this example
json_temp = [{item_id: {"categories": ["/Categories/transportation"]}}]
target_group.categories.assign_to_items(json_temp)

@JimBarry.  This worked for me.  

AdrienLepoutre
New Contributor III

Hi Morten,

It looks like you should be able to do it with the standard Enterprise API with the assign_to_items() command.

That being said, I tried it with different json formats (categories and groupCategories - the second being how it actually is in the json for an item categorized into a group category vs what the example says) and it did not work (always returned "false" response).

Have you had any luck using the REST API? I have also been trying to categorize items into group categories in python.

0 Kudos