HOW TO

Search for a list of tags in a portal using ArcGIS API for Python

Last Published: April 25, 2020

Summary

It is mandatory to include a tag or more when adding items in Portal for ArcGIS and ArcGIS Online. Tags are helpful in finding and identifying specific items in a portal. As more items with tags are added in the portal, it is useful to simplify tag identification. This can be done using the content.search() method in ArcGIS API for Python.

Procedure

The following steps describe the workflow to search for tags in Portal for ArcGIS or ArcGIS Online, and remove duplicate tags.

  1. Import the necessary modules.
from arcgis.gis import GIS
import json
  1. Create a connection to Portal for ArcGIS or ArcGIS Online.
#for ArcGIS Online
gis = GIS("https://arcgis.com", "<username>", "<password>")

#for Portal for ArcGIS
server_url = 'https://<machine>.<domain>.com:6443/arcgis/admin'
gis = GIS('https://test.domain.com/portal', 'portaladmin', 'portal.admin')
ga_server = Server(server_url, gis)
  1. Search for the items in the portal, and modify the max_items parameter to a desired value.
search_result = gis.content.search(query="", max_items =1000)
Note:
If the max_items parameter is not specified, the default value is 10.
  1. Loop through the list of 'search_result' items to find all the tags and populate the results in an array of parameters.
tagList_list = []

for tag in search_result:
     tagList_list.append(tag.tags) 

all_tags = []
for tgs in tagList_list:
    for t in tgs:
        all_tags.append(t)
  1. Sort the tags alphabetically.
all_tags.sort()
  1. Convert the list of tags to a set to identify unique tags and remove duplicates.
unique_tags = set(all_tags)
sorted(unique_tags)
  1. Print all the tags.
print(all_tags)
print("Done")
The following is a sample of the full script for ArcGIS Online.
from arcgis.gis import GIS
import json

gis = GIS("https://arcgis.com", "<username>", "<password>")

search_result = gis.content.search(query="", max_items =1000)

tagList_list = []

for tag in search_result:
     tagList_list.append(tag.tags) 

all_tags = []
for tgs in tagList_list:
    for t in tgs:
        all_tags.append(t)

all_tags.sort()

unique_tags = set(all_tags)
sorted(unique_tags)

print(all_tags)

Article ID:000020473

Software:
  • ArcGIS Online
  • Portal for ArcGIS

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic