I've been trying to achieve the above using an Apps Script snippet I saw on one of the answers (BTW, I am not sure if I should ask this on that thread, or is it ok to open a new one, I am new here :)).
It's in apps, and it working wonderfully on files, but raise havoc with folders.
My problem/questions are: 1) on every ownership change made, the drive is notifying the user, I don't need this, any idea how to shut it down?
2) Folders are been "created" in the new owner root folder, and flat, meaning, if I have a tree with 8 level of sub folders and each has 8 folders in it, I'll get 64 folders in the root of the new owner drive. and those are just "link" they appear along with the appropriate tree structure.
Any idea, suggestions?
Thats the code I used, it may be a bit crude, I am totally a newbie to scriting :)
function main()
{
var rootFolder = DocsList.getFolder('TsT Fol1');
var subFiles = [];
//var subFolderz = [];
subFiles = getFilesInFolder(rootFolder,subFiles);
//subFolderz = GetSubFolders(rootFolder,subFolderz);
for (var i = 0; i < subFiles.length; i++)
{
filename = subFiles[i].getName();
Logger.log(filename);
Logger.log(subFiles[i].getId());
changeOwner
("DestUse@Company.com",subFiles[i].getId());
}
for (var i = 0; i < subFolderz.length; i++)
{
foldername = subFolderz[i].getName();
Logger.log(foldername);
Logger.log(subFolderz[i].getId());
changeFolderOwner
("DestUse@Company.com",subFolderz[i].getId());
}
}
function getFilesInFolder(rootFolder,subFiles) {
var subFolders = rootFolder.getFolders();
var foldersName;
subFiles = subFiles.concat(rootFolder.getFiles())
if (subFolders.length == 0) {return subFiles;}
for (var i = 0; i < subFolders.length; i++)
{
foldersName = subFolders[i];
subFiles = getFilesInFolder(foldersName, subFiles);
}
return subFiles;
};
function GetSubFolders(rootFolder,subFolderz) {
var subFolders = rootFolder.getFolders();
var foldersName;
subFolderz = subFolderz.concat(rootFolder.getFolders())
if (subFolders.length == 0) {return subFolderz;}
for (var i = 0; i < subFolders.length; i++)
{
foldersName = subFolders[i];
subFolderz = GetSubFolders(foldersName, subFolderz);
}
return subFolderz;
};
function changeOwner(newOwnerEmail, fileOrFolderId){
var file = DocsList.getFileById(fileOrFolderId);
var file = DocsList.getFolderById(fileOrFolderId)
var oldOwnerEmail = file.getOwner().getEmail();
if (oldOwnerEmail === newOwnerEmail) {
return;
}
file.removeEditor(newOwnerEmail);
var base = 'https://docs.google.com/feeds/';
var fetchArgs = googleOAuth_('docs', base);
fetchArgs.method = 'POST';
var rawXml = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007'>"
+"<category scheme='http://schemas.google.com/g/2005#kind' "
+"term='http://schemas.google.com/acl/2007#accessRule'/>"
+"<gAcl:role value='owner'/>"
+"<gAcl:scope type='user' value='"+newOwnerEmail+"'/>"
+"</entry>";
fetchArgs.payload = rawXml;
fetchArgs.contentType = 'application/atom+xml';
var url = base + encodeURIComponent(oldOwnerEmail) + '/private/full/'+fileOrFolderId+'/acl?v=3&alt=json';
var content = UrlFetchApp.fetch(url, fetchArgs).getContentText();
}
function changeFolderOwner(newOwnerEmail, fileOrFolderId){
var folder = DocsList.getFolderById(fileOrFolderId)
var oldOwnerEmail = folder.getOwner().getEmail();
if (oldOwnerEmail === newOwnerEmail) {
return;
}
folder.removeEditor(newOwnerEmail);
var base = 'https://docs.google.com/feeds/';
var fetchArgs = googleOAuth_('docs', base);
fetchArgs.method = 'POST';
var rawXml = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007'>"
+"<category scheme='http://schemas.google.com/g/2005#kind' "
+"term='http://schemas.google.com/acl/2007#accessRule'/>"
+"<gAcl:role value='owner'/>"
+"<gAcl:scope type='user' value='"+newOwnerEmail+"'/>"
+"</entry>";
fetchArgs.payload = rawXml;
fetchArgs.contentType = 'application/atom+xml';
var url = base + encodeURIComponent(oldOwnerEmail) + '/private/full/'+fileOrFolderId+'/acl?v=3&alt=json';
var content = UrlFetchApp.fetch(url, fetchArgs).getContentText();
}
function googleOAuth_(name,scope) {
var oAuthConfig = UrlFetchApp.addOAuthService(name);
oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oAuthConfig.setConsumerKey("anonymous");
oAuthConfig.setConsumerSecret("anonymous");
return {oAuthServiceName:name, oAuthUseToken:"always"};
}