HOW TO

Retrieve the folder name that contains a certain item using ArcGIS API for Python

Last Published: April 7, 2023

Summary

Use the Python scripts below to find the folder name for a given item in Portal for ArcGIS.

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']}")

Article ID:000028966

Software:
  • ArcGIS API for Python

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic