Forum Discussion

mja101's avatar
mja101
Explorer | Level 3
2 years ago

Listing every team folder and date last modified

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

 

 

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox 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.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,947 PostsLatest Activity: 6 minutes ago
351 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!