Welcome to the new Box Support website. Check out all the details here on what’s changed.

List all items in folder recursively

New post

Comments

1 comment

  • Rui Barbosa

    Hi Dhruvin.

    First some clarification, the search API end point does not work recursively. As you might expect Box does not store files in "normal" file system, so the concept of folder structure is a virtual one.

    Search actually queries an indexed database of Box content, including name, description, tags, and even the actual content. To prove this if you create a file and immediately search for it, it is likely search won't find it, because it takes a few minutes to index new content.

    To your point of how to recursively list items, you answered your own question, just iterate through the item list and if the item is of type folder, query the end point again recursively.

    You haven't mentioned which stack you're using, so here is an example using the Python SDK:

    from typing import List
    from boxsdk import JWTAuth, Client
    from boxsdk.object.item import Item
    from boxsdk.object.folder import Folder


    class SimpleItem:
        type: str
        id: str
        name: str
        parent_id: str

        def __init__(self, item: Item, parent_id: str = None):
            self.type = item.type
            self.id = item.id
            self.name = item.name
            self.parent_id = parent_id

        def __repr__(self):
            return f"{self.type}\t{self.id}\t{self.name}\t{self.parent_id}\n"


    class CFG:
        """config class"""

        JWT_CONFIG_FILE = ".jwt.config.json"
        AS_USER = "18622116055"
        PARENT_FOLDER_ID = "0"  # folder id 0 is root folder


    def get_box_client(as_user: bool = False):
        """get a box client"""
        auth = JWTAuth.from_settings_file(CFG.JWT_CONFIG_FILE)
        service_client = Client(auth)
        if not as_user:
            return service_client
        user = service_client.user(CFG.AS_USER)
        return service_client.as_user(user)


    def folder_items(folder: Folder) -> List[Item]:
        """list folder items recursively"""
        items = folder.get_items()
        result = []
        for item in items:
            simple_item = SimpleItem(item, folder.id)
            result.append(simple_item)
            if item.type == "folder":
                result.extend(folder_items(item))
        return result


    def main():
        """main function"""

        client = get_box_client(as_user=True)

        # get starting folder
        folder = client.folder(CFG.PARENT_FOLDER_ID).get()

        folder_list = folder_items(folder)

        print(folder_list)


    if __name__ == "__main__":
        main()
        print("\n")
        print("-" * 80)
        print("All Done!")

    The output goes something like this:

      folder 176840203842    Cenotes 172599089223
    , folder        176841790581    2022-10-16      176840203842
    , folder        176838913195    Jane Smith      176841790581
    , file  1037492412345   Box-Dive-Waiver.pdf     176838913195
    , folder        178059063249    2022-10-21      176840203842
    , folder        178059476189    Barbosa 178059063249
    , file  1044375500347   Box-Dive-Waiver.pdf     178059476189
    , file  1044379452138   dan-sample.jpeg 178059476189
    , file  1044391737893   padi-sample.jpeg        178059476189
    , folder        176840211427    Eagle Ray Bay   172599089223
    , folder        176840892622    2022-10-16      176840211427
    , folder        176840808257    Jane Smith      176840892622

     

    Best regards

    0
    Comment actions Permalink

Please sign in to leave a comment.