Categorize items with the API

1047
8
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
8 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.

0 Kudos
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