How To: Retrieve the folder name that contains a certain item using ArcGIS API for Python
Summary
Use the below Python scripts to find the folder name for a given item.
Procedure
In the below example, the item ID is used to find the item and return its folder name:
#libraries import arcgis from arcgis.gis import GIS from arcgis.gis import User #variables portalurl = "https://hostname.domain.com/portal" username = "....." password = "....." #the id of the item that you want to know is in which folder #(you can retrieve the item in any other way you find suitable) itemid = "4f394f2cda81415294f96134220e0c7d" #login gis = GIS(portalurl,username,password,verify_cert=False) #retrieve item item = gis.content.get(itemid) #specify user user = User(gis,username) #retrieve the folder of the item by searching for the item id for f in user.folders: list = user.items(folder=f) for i in list: search_id = list[list.index(i)].id if item.id == search_id: print(f['title'])
Item IDs are unique to the organization, so the script in the example above only prints one folder name. In the below example, the item title is used to find the item. Unlike the item ID, there can be more than one item with the same title in the organization, and the below script returns all the items with that title and their folder names:
#libraries import arcgis from arcgis.gis import GIS from arcgis.gis import User #variables portalurl = "https://hostname.domain.com/portal" username = "........" password = "........" #the title of the item that you want to know is in which folder #(you can retrieve the item in any other way you find suitable) itemtitle= "..........." #login gis = GIS(portalurl,username,password,verify_cert=False) #retrieve item itemsearch = gis.content.search(query=f"title:{itemtitle}") #specify user user = User(gis,username) #retrieve the folder of the item by searching for the item title for f in user.folders: list = user.items(folder=f) for i in list: if i.title == itemtitle: print(f"{i} is in folder {f['title']}")
Related Information
Last Published: 1/19/2023
Article ID: 000028966
Software: ArcGIS API for Python 2.0.1, 2.0, 1.9.1, 1.9, 1.8.5, 1.8.4, 1.8.3, 1.8.2, 1.8.1, 1.8.0, 1.7.1, 1.7.0, 1.6.1, 1.6