Search code examples
javascriptshortcutdrive

Create a folder shortcut in another folder using javascript and Google api v3


I'm trying to create a shortcut of a folder in another folder of my drive using javascript and google api v3, and the code provided by Google is this, but it doesn't work...actually, I don't even understand it. Can someone help me on this?:

var fileMetadata = {
  'name': 'Project Plan',
  'mimeType': 'text/plain'
};
drive.files.create({
  'resource': fileMetadata,
  'fields': 'id'
}, function (err, file) {
  if (err) {
    // Handle error
    console.error(err);
  } else {
    console.log('File Id: ' + file.id);
    shortcutMetadata = {
      'name': 'Shortcut to Project Plan',
      'mimeType': 'application/vnd.google-apps.shortcut'
      'shortcutDetails': {
        'targetId': file.id
      }
    };
    drive.files.create({
      'resource': shortcutMetadata,
      'fields': 'id,name,mimeType,shortcutDetails'
    }, function(err, shortcut) {
      if (err) {
        // Handle error
        console.error(err);
      } else {
        console.log('Shortcut Id: ' + shortcut.id +
                    ', Name: ' + shortcut.name +
                    ', target Id: ' + shortcut.shortcutDetails.targetId +
                    ', target MIME type: ' + shortcut.shortcutDetails.targetMimeType);
      }
    }
  }
});

EXAMPLE OF ITERATIVE PROCESS FOR CREATING A FOLDER STRUCTURE:

  function crearIterativo(original,destino){
          //listo las carpetas que tengo en el original
          gapi.client.drive.files.list({
            'pageSize': 300,
            'q': "mimeType = 'application/vnd.google-apps.folder' and trashed = false and '"+original+"' in parents ",
            'fields': "nextPageToken, files(id, name, parents)"
          }).then(function(response) {
            var files = response.result.files;
            if (files && files.length > 0) {
              for (var i = 0; i < files.length; i++) { //Para cada carpeta encontrada, la creo en destino:
                var file = files[i];
                //informo
                appendPre(file.name + ' (' + file.id + ')' + ' ['+file.parents +']');
                arrayFoldOrig[file.name]=file.id;
                //creo carpeta
                var fileMetadata = {
                  'name' : file.name,
                  'mimeType' : 'application/vnd.google-apps.folder',
                  'parents': [destino]
                };
                gapi.client.drive.files.create({
                  resource: fileMetadata,
                }).then(function(respons) {
                  switch(respons.status){
                    case 200://si va bien, entonces crea estructura
                      var file2 = respons.result;
                      console.log('Created Folder Id: ', file2.id);
                      console.log('Llamo iterativo: ', file2.id);
                      crearIterativo(arrayFoldOrig[file2.name],file2.id);
                      break;
                    default:
                      console.log('Error creating the folder, '+response);
                      break;
                    }
                });
              //
              }
            } else {
              //appendPre('No files found.');
            }
          });
        }

Solution

  • I believe your goal as follows.

    • You want to create the shortcut using Javascript.
    • You have already done the authorization process for creating the file using Drive API.
    • You want to know about how to create the metadata for creating the shortcut.
    • In your script, you want to create the shortcut with gapi.client.drive.files.create.

    For this, how about this answer?

    Modified script:

    Please modify your script as follows.

    From:
    var fileMetadata = {
      'name' : file.name,
      'mimeType' : 'application/vnd.google-apps.folder',
      'parents': [destino]
    };
    
    To:
    const targetId = "###"; // Please set the target file ID or folder ID.
    var fileMetadata = {
      'name': file.name,
      'mimeType': 'application/vnd.google-apps.shortcut',  // Modified
      'parents': [destino],
      'shortcutDetails': {'targetId': targetId},  // Added
    };
    
    • Please use application/vnd.google-apps.shortcut as the mimeType.
    • Please set the target file ID or folder ID to targetId. For example, when you want to create the shortcut of the folder A, please set the folder ID of the folder A to targetId.
    • In above case, the shortcut is created to the folder with the folder ID of destino.

    References: