Select to view content in your preferred language

Getting all folders from the GIS object

12-20-2022 08:05 AM
Labels (3)
Occasional Contributor II


I am busy experimenting with cloning content from ArcGIS Online to Portal, using this very useful page:

However, I am running into a piec of caode that does not work, it's the piece where hosted feature layers are cloned into a recreated folder structure, where it's the last part, recreating the folder structure, that's giving me problems. More specifically, it is this piece of code:

for hfs in hosted_fsvc:


        if hfs.ownerFolder:

            folder = next((f['title']

                                    for f in

                                    if f['id'] == hfs.ownerFolder))

I suppose this code should loop throught all folders, for every hosted feature layer, and find the one where the layer resides. However, will only find the folders that my admin user is the owner (or creator?) of.

So the question is, how would I loopt through ALL folders?


Joris Frenkel


Tags (2)
0 Kudos
2 Replies
Occasional Contributor III

Hi @jorisfrenkel 

You can loop through users and their folders using the below snippet, and access a list of feature services within each folder. Just extend the number of max users to cover the amount of usernames necessary.


from arcgis import GIS

gis = GIS("home")

users =

for user in users:
    folders = user.folders
    for folder in folders:
        folder_name = folder["title"]
        feature_services = [item for item in user.items(folder_name) if item.type == "Feature Service"]
        for fs in feature_services:
            print("\t", fs.title)


Occasional Contributor II


I am currently busy with other things, but I will look into it when I have the time.


0 Kudos