Google Drive API VB.NET Parent Folder of a Folder

1.5k views Asked by At

I am trying to work with Google Drive API with vb.net. I have seen a lot of posts,but I am not able to understand how to choose the right folder to add a file.

I have a lot of "Backup" folders but, I assume now, I want to add a file into "Backup" folder under "MyApp1" folder.

I am doing it this way but I don't know how to continue. I search all of my "Backup" folder and I need to check which is under parent folder "Myapp".

    Public Shared Function TheRightFolder() As Boolean

    Try
        Dim FolderName As String = "Backup"
        Dim ParentName As String = "MyAPP1"
        Dim result As Boolean = False

        Dim request = Service.Files.List()
        Dim requeststring As String = ("mimeType='application/vnd.google-apps.folder' And trashed=false and title='" & FolderName & "'")
        request.Q = requeststring
        Dim Parent = request.Execute()

        For Each item As File In Parent.Items
            'Get PARENT NAME OF THE FOLDER TO CHECK IF I AM INSERT IN MY FILE IN THE RIGHT DIRECTORY (USING 'item.Parents DATA'?)
            '...
            If ParentName = new_func_to_get_parent_folder_name Then
                Return True
            End If
        Next
    Catch EX As Exception
        MsgBox("ERROR" & EX.Message)
    End Try
    Return False

End Function

What am I doing wrong?

Do I need to change way to think to get the right folder?

Thanks in advance!

2

There are 2 answers

0
chriswaddle On BEST ANSWER

Found solution:

Public Shared Sub ParentsInFolder(ByVal folderId As String)

    Dim request As ParentsResource.ListRequest = Service.Parents.List(folderId)
    Try
        Dim parents As ParentList = request.Execute()
        For Each parent As ParentReference In parents.Items
            Dim FileId As String = parent.Id.ToString
            InfoFromID(Service, FileId)
        Next
    Catch e As Exception
        MsgBox("Errore: " & e.Message)
    End Try

End Sub

And:

    Public Shared Sub InfoFromID(ByVal Service As DriveService, fileId As String)
    Try
        Dim file As File = service.Files.Get(fileId).Execute

        Dim Title As String = file.Title
        Dim MimeType As String = file.MimeType.ToString
        Dim description As String = file.Description

    Catch e As Exception
        MsgBox("Errore: " + e.Message)
    End Try
End Sub
0
jorge On

You have to share the created folder.

'Comparte la carpeta creada

Dim userPermission As Permission = New Permission()
userPermission.Type = "anyone"
userPermission.Role = "writer"
Dim request4 = eDRIVEService.Permissions.Create(userPermission, Results2.Files.Item(0).Id.ToString)
request4.Fields = "id"
request4.Execute()