Search code examples
google-drive-apidrive

Find file's previous parent folder in Google Drive


Let's say I have folder A and folder B. File.txt is stored in folder A and moved to folder B. Given the file, is it possible to know the previous parent folder of the file? It seems to be possible to know witch folder the file has been moved to, but not the origin folder.


Solution

  • You can use Google Drive Activity API which allows you to retrieve information about the changes made within a user's Google Drive.

    Using activity.query()

    Making Requests in the Drive Activity API

    There are two ways to request activity: by Drive item, or for everything underneath a folder hierarchy.

    • itemName: The format for this key is "items/ITEM_ID". Typically this is a file in Drive. If you specify a folder for this key, it will show activity for the folder itself, such as when the folder was created or renamed.

    • ancestorName: The format for this key is "items/FOLDER_ID", and the response will include activity on all items in the subtree underneath this folder.

    When no key is set, it defaults to using an ancestor name of "items/root", which will show activity for all items in your Google Drive.

    Filtering

    You can restrict the actions which might be returned in the DriveActivity object by constructing a filter string in the request.

    To restrict by action type, use the field name detail.action_detail_case with the "has" operator (:) and either a singular value or a list of allowed action types enclosed in parentheses. Examples include:

    detail.action_detail_case: RENAME
    detail.action_detail_case:(CREATE UPLOAD)
    -detail.action_detail_case:MOVE
    

    These filtering conditions can be combined within a single filter string.


    Sample Request Body:

    {
      "filter": "detail.action_detail_case:MOVE",
      "itemName": "items/1kNGhKfVBtNHDNZPxUEzHYxxxxxx"
    }
    
    • Filter all drive activity that has move ActionDetail
    • Primary criteria is to return activities for a specific file with file id: 1kNGhKfVBtNHDNZPxUEzHYxxxxxx

    Response Body:

    This will return a DriveActivity object that will contain a Move object under ActionDetail object. You can refer to the removedParents->driveItem to get the information regarding the previous parent folder of the file.

    {
      "activities": [
        {
          "primaryActionDetail": {
            "move": {
              "addedParents": [
                {
                  "driveItem": {
                    "name": "items/1TrX6KcAJppWCj9GSUjSYn79Aqxxxx",
                    "title": "NewFolder",
                    .....
                  }
                }
              ],
              "removedParents": [
                {
                  "driveItem": {
                    "name": "items/1YUrD6lUshY2IG0fIi0aFUoQRxxxx",
                    "title": "Untitled folder",
                    .....
                  }
                }
              ]
            }
          },
          ......
          ],
          "actions": [
            {
              ......
            }
          ],
          "targets": [
            {
              "driveItem": {
                "name": "items/1kNGhKfVBtNHDNZPxUEzHYxxxxxx",
                "title": "sampledoc.json",
                ......
              }
            }
          ],
          "timestamp": "2021-03-16T16:04:24.072Z"
        }
      ]
     }