How To: Find specific items using queries in the arcgis.gis module with the ArcGIS Python API


When using the ArcGIS for Python API in various workflows such as Clone, Export, and so forth, it  may be necessary to find specific items in ArcGIS Online or Portal for ArcGIS and perform an action on only those items. To do this, the search function in arcgis.gis module can be used. It is also possible to use different queries or a combination of different queries to find and use certain items. 

This article shows some examples of how to use queries to find items in AGOL or Portal using ArcGIS for Python API.  



Examples to use query in Search function to return specific items in ArcGIS Online

  • Search for all content:
items = gis.content.search(query='')
  • Search for content by keyword:
items = gis.content.search(query='KEYWORD')
  • Search for content by owner:
items = gis.content.search(query='owner:YOUR USERNAME')
items = gis.content.search(query='NOT owner:YOUR USERNAME')
  • Search for content by owner when username is already provided as a parameter:
username = "YOUR USERNAME"
items = gis.content.search(query='owner:'+username)
items = gis.content.search(query='owner:{}'.format(username)
  • Search for Feature Layers owned by the logged-in user:
items = gis.content.search(query="owner:" + gis.users.me.username)
  • Search for content by title:
items = gis.content.search(query='title:TITLE')
  • Search for content that begin with a prefix:

items = gis.content.search(query='title:TITLE*')
  • Search for content by item type
items = gis.content.search(query='', item_type='Feature Service')
items = gis.content.search(query='', item_type='Web Map')
items = gis.content.search(query='', item_type='Web Mapping Application')
  • The following query returns both Web Maps and Web Mapping Applications
items = gis.content.search(query='', item_type='Map')
  • Search for content outside the organization (query cannot be blank)
items = gis.content.search(query='title:TITLE', outside_org=True)
  • Combine query parameters
items = gis.content.search(query='title:TITLE, owner:YOUR USERNAME', item_type='Feature Layer')
items = gis.content.search(query='title:TITLE, type:map, owner:YOUR USERNAME')

Related Information

Last Published: 11/5/2020

Article ID: 000024383