I recently ran across a question on Stack overflow. The question was quite simple how to retrieve the folder values of from Google drive and display them in a directory list using C# and the Google .Net client library.
I have used a PageStreamer in the ListAll method in the event that there are more then 1000 files in the directory this will ensure that we get them all back. PageStreaming is much easier then having to deal with the nextPageToken yourself.
After we have all of the results then PrettyPrint is running recursively to request all of the files from within any directories.
Conclusion
By using PageStreamer you can retrieve all of the rows for your request rather then having to worry about the nextPageToken yourself.
Note: I am not responsible for the usage of your quota if you print everything 🙂
Hi Linda,
Thanks for all your examples and explanations. It helped me a lot.
I have a question.
I have up to 3000 files on my Drive (service account).
I have to show some kind of Explorer with the files and folders.
What would you recommend?
Read all files at startup and populate the explorer with the result. or
Build the explorer on demand from root. Which could result in more request.
NB.
Do I need to build an Exponential Backoff, when using the “PageStreamer”?
Thx
The client library has built in support for back-off. You shouldn’t need to worry about it, we have it taken care of.
There is a few things you need to consider with this. The first being memory. Whatever you return there needs to be enough memory in your system to hold the object. Currently you only have 3000 files which shouldnt be a problem just requesting them all in the start and using them as you go. If you start to get more files and things begin to slow down you may have to revisit it.
Hello,
I want to list files which are owned by that particular user only, not shared with user. What scope I have to set?
you need to create a new FilesListOptionalParms and set Q to.
Set the email of the address of the user who you are looking for.
Thank you for the response but it gives Bad Request error.
I am using google drive api, it is working fine when I am using locally, but when I host in my local IIS, then it is not working,
protected void btnList_Click(object sender, EventArgs e)
{
String CLIENT_ID = “”;
String CLIENT_SECRET = “My_CLIENT_SECRET”;
string[] scopes = new string[] { DriveService.Scope.Drive,
DriveService.Scope.DriveFile};
string jsonpath = Server.MapPath(“~/App_Data/”);
//////// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
//IAuthorizationCodeFlow credential = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
//{
// ClientSecrets = new ClientSecrets
// {
// ClientId = CLIENT_ID,
// ClientSecret = CLIENT_SECRET
// },
// Scopes = new[] { DriveService.Scope.Drive },
// DataStore = new FileDataStore(jsonpath)
//});
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync
(new ClientSecrets
{
ClientId = CLIENT_ID,
ClientSecret = CLIENT_SECRET
}
, scopes
, Environment.UserName
, CancellationToken.None
, new FileDataStore(jsonpath)).Result;
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = “Drive API Quickstart”,
});
string emlid = txtMailId.Text.Trim();
string Q = “‘” + emlid + “‘ in writers”;
IList _Files = new List();
_Files = GetFiles(service, Q);
List objGDriveFileList = new List();
foreach (File item in _Files)
{
GDriveFile objGDriveFile = new GDriveFile();
if (item.MimeType == “application/vnd.google-apps.document”)
{
objGDriveFile.FileName = item.Title;
objGDriveFile.FileLink = item.AlternateLink;
objGDriveFileList.Add(objGDriveFile);
}
}
GvGDriveFileList.DataSource = objGDriveFileList;
GvGDriveFileList.DataBind();
}
public static IList GetFiles(DriveService service, string search)
{
IList Files = new List();
try
{
//List all of the files and directories for the current user.
// Documentation: https://developers.google.com/drive/v2/reference/files/list
FilesResource.ListRequest list = service.Files.List();
list.MaxResults = 1000;
if (search != null)
{
list.Q = search;
}
FileList filesFeed = new FileList();
filesFeed = list.Execute();
//// Loop through until we arrive at an empty page
while (filesFeed.Items != null)
{
// Adding each item to the list.
foreach (File item in filesFeed.Items)
{
Files.Add(item);
}
// We will know we are on the last page when the next page token is
// null.
// If this is the case, break.
if (filesFeed.NextPageToken == null)
{
break;
}
// Prepare the next page of results
list.PageToken = filesFeed.NextPageToken;
// Execute and process the next page request
filesFeed = list.Execute();
}
}
catch (Exception ex)
{
// In the event there is an error with the request.
// Console.WriteLine(ex.Message);
}
return Files;
}
public class GDriveFile
{
public string FileName { get; set; }
public string FileLink { get; set; }
}
Web apps have a different authentication flow then installed applications. Most of my samples are for installed applications you may want to check this Web applications (ASP.NET MVC)
I’m wondering…
Why don’t use the PICKER-API with ViewId:DOCS ?
https://developers.google.com/picker/docs/
IMO an easier solution for Web-Apps.
I am not a web developer I am a backend developer. This is not going to work for me on a server running a background process. That and this tutorial is for the Google Drive API. The google picker APIs a separate API which if i had it would be part of a different tutorial series on Google picker.
Hi Linda,
Is there any way to get Files and Folders from Computers Folder instead of my drive.
I tried your script it shows only the files and folders present in the my drive.
I want all files and folders which are synced from all different computers in computers menu.
Why I am Using this because I want to compare system files with google drive synced files using c# so that i can check which are files uploaded.
Can you please help me out. I am new to coding
that is beyond the scope of this tutorial sorry. try stackoverflow.com