Getting all folders from the GIS object

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

Hi,

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

https://developers.arcgis.com/python/guide/cloning-content/#the-cloning-process

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:

    try:

        if hfs.ownerFolder:

            folder = next((f['title']

                                    for f in source.users.me.folders

                                    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, source.users.me.folders 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?

Regards,

Joris Frenkel

 

Tags (2)
0 Kudos
2 Replies
Clubdebambos
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 = gis.users.search(max_users=10)

for user in users:
    print(user)
    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"]
        print(folder_name)
        for fs in feature_services:
            print("\t", fs.title)

 

~ learn.finaldraftmapping.com
jorisfrenkel
Occasional Contributor II

Thanks!

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

Joris

0 Kudos