soni arora
soni arora

Reputation: 13

google drive api create sub folders

I need to create sub folders in google drive using google drive api added using nuget package in console application.

I can get the folder id of root folder. Can get children of rot folder, can also upload file in root folder. Only problem is creation of sub folders in folders.

for (int i = 1; i < array.Count(); i++)
                        {
                            var subfoldername = new Google.Apis.Drive.v2.Data.File { Title = array[i], MimeType = "application/vnd.google-apps.folder" };
                            ChildrenResource.ListRequest request = service.Children.List(rootfolderid);
                            ChildList children = request.Execute();
                            if (children.Items.Count > 0)
                            {
                                foreach (ChildReference c in children.Items)
                                {
                                    Google.Apis.Drive.v2.Data.File file = service.Files.Get(c.Id).Execute();
                                    if (file.MimeType == "application/vnd.google-apps.folder")
                                    {
                                        List<GoogleDriveFile> googledrive = new List<GoogleDriveFile>();
                                        googledrive.Add(new GoogleDriveFile
                                        {
                                            OriginalFilename = file.OriginalFilename
                                        });
                                    }
                                }
                            }
                            else
                            {
// here need to add sub folder in folder, but this line adds folder at root
                                var result = service.Files.Insert(foldername).Execute();
                            }

Upvotes: 1

Views: 6756

Answers (2)

Mike
Mike

Reputation: 177

Here is the way i do, when creating a sub folder in google drive it must need a parents. So before execute the string q, we need to search for the parent root id

string findRootId = "mimeType = 'application/vnd.google-apps.folder' and title ='" + RootFolder + "' and trashed = false";

IList<File> _RootId = GoogleDriveHelper.GetFiles(service, findRootId);

if (_RootId.Count == 0) {                                  
  _RootId.Add(GoogleDriveHelper.createDirectory(service, RootFolder, "", "root"));
  Console.WriteLine("Root folder {0} was created.", RootFolder);
}

var id = _RootId[0].Id;

string Q = "mimeType = 'application/vnd.google-apps.folder' and '" + id + "' in parents and title ='" + GoogleDriveFolderName + "' and trashed = false";

Upvotes: 2

Mr.Rebot
Mr.Rebot

Reputation: 6791

You must add the property parents while creating a Folder.

parents[]

Collection of parent folders which contain this file. Setting this field will put the file in all of the provided folders. On insert, if no folders are provided, the file will be placed in the default root folder.

Sample Code:

function createSubFolder() {
var body = new Object();
body.title = 'SubFolder';
body.parents = [{'id':'0B5xvxYkWPFpCUjJtZVZiMWNBQlE'}];
body.mimeType = "application/vnd.google-apps.folder";

console.log(body)
var request = gapi.client.request({
'path': '/drive/v2/files',
'method': 'POST',
'body': JSON.stringify(body)
});

request.execute(function(resp) { console.log(resp); });
}

I'm using Drive v2 in JavaScript

Hope this helps

Upvotes: 2

Related Questions