Dylan
Dylan

Reputation: 21

Changing the owner of a folder & files within that folder

I'm fairly new to scripting and would like to know if it is at all possible to run a google apps script to changing the owner permissions of a folder and subfolders/files to another user?

Currently, I have tried this

function myFunction() {
 var folder = DriveApp.('Shared Folder');
folder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);
 folder.setOwner("new email owner")
 }

This allows me to make someone else owner of a created folder, But i am not sure of how to do this for an existing folder (and also its sub folders and files).

Also, these files will be owned by the google apps administrator (before permissions change)

Upvotes: 2

Views: 2497

Answers (1)

pointNclick
pointNclick

Reputation: 1642

Based on your description, the following function would work fairly well for your scenario:

function folderTransfer()
{
  var fol = DriveApp.getFolderById("**************");
  var files = fol.getFiles();
  var me = "[email protected]";
  var newOwner = "[email protected]";

  fol.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
  fol.setOwner(newOwner);
  fol.removeEditor(me);

  while (files.hasNext())
  {
    var file = files.next();
    file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
    file.setOwner(newOwner);
    file.removeEditor(me);
  }
}

Remove owner permission is optional but it depends on your use case in this scenario. Taking the code above, if I dont remove myself as the owner, I will still be able to see and edit the doc even though the owner is now [email protected] in this case. However, since the permission for all others are set to view only, they will have view only access.

Please note, this is a simple function which allows me to transfer the owner ship of files in a single folder. However, a similar function such as the getFiles() function should also allow you to getFolders() as well. Transfer the while loop to a different function and passing the folder ID to iterate through the files in that folder and change their ownership in a similar way. In the second case, you code will look something like this:

//Transfering all files to a new owner
function fileTransfer(folderID, owner, newOwner)
{
  var folder = DriveApp.getFolderById(folderID);
  var files = folder.getFiles();

  while (files.hasNext())
  {
    var file = files.next();
    file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
    file.setOwner(newOwner);
    file.removeEditor(owner);
  }
}

function folderTransfer()
{
  var fol = DriveApp.getFolderById("************************");
  var files = fol.getFiles();
  var me = "[email protected]";
  var newOwner = "[email protected]";

  var folders = fol.getFolders();
  while (folders.hasNext())
  {
    var folder = folders.next();
    var folderID = folder.getId();
    fileTransfer(folderID, me, newOwner);
    folder.setOwner(newOwner);
    folder.removeEditor(me);
  }
  //Transferring parent folder to newOwner
  fol.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
  fol.setOwner(newOwner);
  fol.removeEditor(me); 
}

At what granular level and how many nested folders do you have that you want to perform the function on and how to iterate through those, is up to you. But this should give you a fair idea of how it could be done.

Hope this helps.

Upvotes: 1

Related Questions