3

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 != 'adminuser@domain.co.uk'){
    file.setOwner('adminuser@domain.co.uk');

}
  Logger.log(file);

}
}
John Smith
  • 387
  • 2
  • 8
  • 24
  • Possible duplicate of [Transfer ownership of a file to another user in Google Apps Script](https://stackoverflow.com/questions/10796464/transfer-ownership-of-a-file-to-another-user-in-google-apps-script) – Rubén Jun 01 '18 at 02:44

3 Answers3

1

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.

Community
  • 1
  • 1
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • That's actually not true, "If you're the domain admin, you can use the Drive SDK (now available as an advanced service on Apps Script) to impersonate your users and call setOwner as them" – John Smith Oct 23 '14 at 15:31
  • 1
    Calling a person a liar is serious. You've included a quote without attribution, and not from this answer. Can you be more succinct about what is not factually correct? – Mogsdad Oct 23 '14 at 15:36
  • I never called you anything, I declared that you statement was not true =), you have commented the exact same thing in that thread here it is: http://stackoverflow.com/questions/23783735/take-ownership-of-other-users-drive-files-using-google-apps-script – John Smith Oct 23 '14 at 15:45
  • **Which statement** is not true, and what is wrong? The issue you describe has been reported. It is issue 2756. Text in that issue can be summarized as stated. It is possible for an admin to use the Drive API to impersonate users. It's been done ([another ref](http://stackoverflow.com/questions/13652706), [and another](http://stackoverflow.com/a/12916795), [and another](https://pythonhosted.org/gdata/docs/impersonating.html)), and there is recently updated documentation for [delegation of authority](https://developers.google.com/drive/web/delegation) for the Drive API. – Mogsdad Oct 23 '14 at 16:44
0

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 != 'dummyName@gmail.com') {
    //file.setOwner('dummyName@gmail.com');
  //}
}
}

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 != 'someEmail@gmail.com') {
      file.setOwner('someEmail@gmail.com');
      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

Alan Wells
  • 30,746
  • 15
  • 104
  • 152
0

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

Community
  • 1
  • 1