John Smith
John Smith

Reputation: 397

Trying to change owner on files in Google Drive

I'm trying to change ownership of documents in my Drive, but I recieve the following error:

We're sorry, a server error occurred. Please wait a bit and try again. (line 12, file "Code")

function transferFiles() {

var files = DriveApp.getFiles();
while (files.hasNext()) {
  var file = files.next();
var owner = file.getOwner().getEmail();

  if (owner != '[email protected]'){
    file.setOwner('[email protected]');

}
  Logger.log(file);

}
}

Upvotes: 3

Views: 3187

Answers (3)

Domain administrators are not allowed to change file/folder property, only onwer can do that. What you can do is to implement a webapp that will let the user (that is also the owner) automatically do that, based on time or any event you can trigger. Take a look to: Google Apps Script Web App Example, Take ownership of other users Drive-files using Google Apps Script

Upvotes: 0

Alan Wells
Alan Wells

Reputation: 31300

I think that the root cause of that error msg might not be something like your internet connection. I'm guessing that there is an error trying to set the permissions, which terminates your code, and then something happens with the server connection.

I'm not saying this is an answer to the problem, but I ran some code myself and I'm getting files that don't even show up in my Google Drive. I have no idea where these files are. These files that are showing up in my list of files have other owners, and are not in my Google Drive. So, I'm assuming that they are files that I previously downloaded, and probably gave permissions to, then deleted. I don't know.

I wanted to know which file was causing the error, what folder the file was in, and what the file count is. So, I created a counter and looked up the folder that the file is in. The files that show up in the list, also are in a folder that DOES NOT EXIST in my Google drive. So, again, DriveApp is getting files from somewhere, and from some folder that is unknown to me.

function transferFiles() {

var files = DriveApp.getFiles();
var cntFiles = 0;

while (files.hasNext()) {
  cntFiles = cntFiles + 1;
  Logger.log('File Count: ' + cntFiles)

  var file = files.next();
  Logger.log('file: ' + file);

  var whatFolder = file.getParents();

  while (whatFolder.hasNext()) {
    var folder = whatFolder.next();
    Logger.log('Folder Name: ' + folder.getName());
  }

  var owner = file.getOwner().getEmail();
  Logger.log('owner: ' + owner);

  //if (owner != '[email protected]') {
    //file.setOwner('[email protected]');
  //}
}
}

I'm getting files returned that are from other Google accounts that are also mine. Weird. How is it doing that? Google must have somehow linked my different Google accounts. My point is, to figure out what this problem is, maybe you need to know exactly what file is giving the error.

You could add some error handling to your code, so that a failure doesn't break the code. You'd want to know what file failed to allow the permission to be changed. So You'd probably want to log that somehow.

Here is code that shows all the files that caused an error, and what the error was for that file:

function transferFiles() {

var files = DriveApp.getFiles();
var cntFiles = 0;

while (files.hasNext()) {
  cntFiles = cntFiles + 1;
  //Logger.log('File Count: ' + cntFiles)

  var file = files.next();
  //Logger.log('file: ' + file);

  var whatFolder = file.getParents();

  while (whatFolder.hasNext()) {
    var folder = whatFolder.next();
    //Logger.log('Folder Name: ' + folder.getName());
  }

  var owner = file.getOwner().getEmail();
  //Logger.log('owner: ' + owner);

  try { 
    if (owner != '[email protected]') {
      file.setOwner('[email protected]');
      Logger.log('File that was changed: ' + file);
    }
  } catch (theError) {
    Logger.log('There was an error for file: ' + theError);
    Logger.log('File that caused error: ' + file);
  };
}
}

I'm getting errors:

Action not allowed Invalid argument: sharing.user

Upvotes: 0

Mogsdad
Mogsdad

Reputation: 45710

You've described a reported issue. See & star Issue 2756: Server error for illegal ACL change

Summary: Only the owner of a file can change ownership. Admin accounts don't own user's files, so they have no special privileges in this respect.

A possible work-around utilizing the Drive API (not Google Apps Script) to impersonate domain users is described in this answer, unfortunately without implementation details.

Upvotes: 1

Related Questions