cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Are you interested in learning how media industry leaders use Dropbox Replay? Register for our webinar here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Listing every team folder and date last modified

Listing every team folder and date last modified

mja101
Explorer | Level 3

I am trying to list every dropbox team folder and a date against it that specifies the last time a file within was modified. 

 

I have been successful in listing folders with the below script however, the problem is that the folders being listed are only what the "as_admin" account has access to. Is there a way using the admin account to list everything regardless of folder access? I have full control of the api scope so if there is something there, I can easily change it.

 

 

def establish_connection():
    # Create an instance of the Dropbox API client
    try:      

        dbx_team = dropbox.DropboxTeam(os.environ['ACCESS_TOKEN'])
        # get the team member id for common user
        members = dbx_team.team_members_list()

        dbx_team = dropbox.DropboxTeam(os.environ['ACCESS_TOKEN']).as_admin("")

    except AuthError as e:
        print(f"Error connecting to Dropbox API: {e}")
        exit()
    return dbx_team


def list_team_folders():
    
    dbx_team = establish_connection()

    
    folders = []
    result = dbx_team.files_list_folder(path='')
    while True:
        for entry in result.entries:
            if isinstance(entry, dropbox.files.FolderMetadata):
                folders.append(entry.path_display)
        if not result.has_more:
            break
        result = dbx_team.files_list_folder_continue(result.cursor)

    # Iterate over each folder and get the latest modified date of its files
    for folder in folders:
        latest_modified = datetime(1970, 1, 1) # Set to an arbitrary old date
        result = dbx_team.files_list_folder(path=folder)
        while True:
            for entry in result.entries:
                if isinstance(entry, dropbox.files.FileMetadata):
                    # Check if this file was modified more recently than the current latest modified date
                    modified = datetime.strptime(str(entry.client_modified), '%Y-%m-%d %H:%M:%S')
                    if modified > latest_modified:
                        latest_modified = modified
            if not result.has_more:
                break
            result = dbx_team.files_list_folder_continue(result.cursor)
        print(f'{folder}: {latest_modified}')

 

 

1 Reply 1

Greg-DB
Dropbox Staff

As you found, starting from files_list_folder(path='') will only list the contents of that particular member's folder; if there are other team folders that the member doesn't have mounted in their account, those team folders won't be found that way.

 

You can list all team folders for a team using team_team_folder_list/team_team_folder_list_continue though. (Similarly, if you want to list all "namespaces", which includes team folders, you can use team_namespaces_list/team_namespaces_list_continue.) These methods are called using a DropboxTeam instance, not a Dropbox instance, so you would call them without the 'as_admin'.

 

Then, you can start listing any arbitrary team folder like this:

dbx_team.as_admin(admin_id).files_list_folder("ns:" + team_folder_id)

 

And for reference, if you need to list a "team space", refer to the Team Files Guide.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?