Death The Kid
Death The Kid

Reputation: 11

How to recursively retrieve folder with smartsheet API

I'm working on a backup tool for smartsheet in python. I want to retrieve all the users file on my domain.

I don't know how to get the data but i don't know how to keep the Tree of folders like:

Folder A:  
    - File A  
    - Folder AB:
        *File AZ
        *File AE
        *Folder AC:
            File E

This above example is only subjective.

With Smartsheet when you retrieve the workspaces with :

ss.Home.list_all_contents()

here ss is smartsheet.Smartsheet(token)

You got that in return:

{
"folders": [
    {
        "id": 5709073300645764,
        "name": "folder 1",
        "permalink": "https://app.smartsheet.com/b/home?lx=Dsje3YKtpyZScrCX6Z1"
    }
],
"sheets": [
    {
        "id": 4583173393803140,
        "name": "sheet 1",
        "accessLevel": "OWNER",
        "permalink": "https://app.smartsheet.com/b/home?lx=Dsje3YKtpyZScrCX6Za", 
        "createdAt": "2015-06-05T20:05:29Z",
        "modifiedAt": "2015-06-05T20:05:43Z"
    },
    {
        "id": 2331373580117892,
        "name": "Copy of sheet 1",
        "accessLevel": "OWNER",
        "permalink": "https://app.smartsheet.com/b/home?lx=Dsje3YKtpyZScrCX6Zb",
        "createdAt": "2015-06-05T20:05:29Z",
        "modifiedAt": "2015-06-05T20:05:43Z",
        "source": {
            "id": 4583173393803140,
            "type": "sheet"
        }
    }
],
"reports": [],
"templates": [],
"workspaces": [],
"sights": []
}

But nested folder looks like that:

{
    "id": 138433004889988,
    "folders": [
        {
            "id": 672830015727492,
            "folders": [
                {
                    "id": 3791044992100228,
                    "folders": [],
                    "reports": [],
                    "favorite": null,
                    "name": "3nested",
                    "templates": [],
                    "permalink": "",
                    "sheets": []
                }
            ],
            "reports": [],
            "favorite": null,
            "name": "2nested",
            "templates": [],
            "permalink": "",
            "sheets": [
                {
                    "accessLevel": "OWNER",
                    "userSettings": null,
                    "id": 2308634748184452,
                    "fromId": null,
                    "modifiedAt": "2017-08-26T07:30:23+00:00",
                    "attachments": [],
                    "resourceManagementEnabled": null,
                    "projectSettings": null,
                    "effectiveAttachmentOptions": [],
                    "favorite": null,
                    "showParentRowsForFilters": null,
                    "createdAt": "2017-08-07T08:17:20+00:00",
                    "version": null,
                    "rows": [],
                    "columns": [],
                    "readOnly": null,
                    "name": "file 2nested",
                    "permalink": "",
                    "owner": null,
                    "totalRowCount": null,
                    "ganttEnabled": null,
                    "source": null,
                    "dependenciesEnabled": null,
                    "ownerId": null,
                    "discussions": []
                }
            ]
        }
    ],
    "reports": [],
    "favorite": null,
    "name": "fnfd",
    "templates": [],
    "permalink": "",
    "sheets": [
        {
            "accessLevel": "OWNER",
            "userSettings": null,
            "id": 8542174187939716,
            "fromId": null,
            "modifiedAt": "2017-08-26T07:30:24+00:00",
            "attachments": [],
            "resourceManagementEnabled": null,
            "projectSettings": null,
            "effectiveAttachmentOptions": [],
            "favorite": null,
            "showParentRowsForFilters": null,
            "createdAt": "2017-08-03T18:16:29+00:00",
            "version": null,
            "rows": [],
            "columns": [],
            "readOnly": null,
            "name": "msp",
            "permalink": "",
            "owner": null,
            "totalRowCount": null,
            "ganttEnabled": null,
            "source": null,
            "dependenciesEnabled": null,
            "ownerId": null,
            "discussions": []
        }
    ]
}

the Tree of above json looks like that:

folder fnfd:  
    - File msp.xlsx
    - Folder 2nested:
        -File file 2nested.xlsx
        -Folder 3nested

Question

How Can I retrieve recursively all those folder no matter the numbers of folder nested that you need to retrieve

Upvotes: 0

Views: 755

Answers (1)

Steve Weil
Steve Weil

Reputation: 873

You will need a standard recursive descent algorithm. For each call that returns a list of folders, you'll need to loop through the folder IDs and call ss_client.Folders.get_folder(folder_id) to retrieve the list of contained sheets and any child folders.

Upvotes: 2

Related Questions