File movement between Google Shared Drive (team drive) folders using Apps Script

Okay looks like I found an answer, via REST API I can update file’s parents. I’ve made that call and it’s working.

Here’s the sample.

var apiUrl = "https://www.googleapis.com/drive/v3/files/fileId?addParents=newFolderId&removeParents=oldFolderId&supportsTeamDrives=true";
var token = ScriptApp.getOAuthToken();
var header = {"Authorization":"Bearer " + token};
var options = {
"method":"PATCH",
"headers": header
};
var res = UrlFetchApp.fetch(apiUrl, options);

UPDATE
Using Advance Services API we can achieve the same, here’s the answer I’ve received from Aliaksei Ivaneichyk

function moveFileToFolder(fileId, newFolderId) {  
  var file = Drive.Files.get(fileId, {supportsTeamDrives: true});

  Drive.Files.patch(file, fileId, {
    removeParents: file.parents.map(function(f) { return f.id; }),
    addParents: [newFolderId],
    supportsTeamDrives: true
  });
}

Here you need to Enable Drive SDK advance services if you are using Appscript. In case of Appmaker Add Drive SDK as Service in Settings Option.

Leave a Comment