I'm trying to the thumbnails for the most recent photos from OneDrive using the Microsoft Graph API.
I've been using Microsoft Graph OneDrive Photo Browser sample on GitHub as a guide and I'm trying to modify it to show the most recent photos only.
I need help with two things:
- Expand subfolders. I'm not sure if this is the correct terminology but if I make a request like this (https://graph.microsoft.com/v1.0/me/drive/special/photos/children?select=id,name) it will return a collection of folders. I want the response to contain collection of photo thumbnails rather than having to make another request for each folder to get those thumbnails.
- Sort the request by creation date in descending order.
Here's the code from the photo browser sample app.
using Models;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Graph;
public class ItemsController
{
private GraphServiceClient graphClient;
public ItemsController(GraphServiceClient graphClient)
{
this.graphClient = graphClient;
}
/// <summary>
/// Gets the child folders and photos of the specified item ID.
/// </summary>
/// <param name="id">The ID of the parent item.</param>
/// <returns>The child folders and photos of the specified item ID.</returns>
public async Task<ObservableCollection<ItemModel>> GetImagesAndFolders(string id)
{
ObservableCollection<ItemModel> results = new ObservableCollection<ItemModel>();
IEnumerable<DriveItem> items;
var expandString = "thumbnails, children($expand=thumbnails)";
// If id isn't set, get the OneDrive root's photos and folders. Otherwise, get those for the specified item ID.
// Also retrieve the thumbnails for each item if using a consumer client.
var itemRequest = string.IsNullOrEmpty(id)
? this.graphClient.Me.Drive.Special["photos"].Request().Expand(expandString)
: this.graphClient.Me.Drive.Items[id].Request().Expand(expandString);
var item = await itemRequest.GetAsync();
items = item.Children == null
? new List<DriveItem>()
: item.Children.CurrentPage.Where(child => child.Folder != null || child.Image != null);
foreach (var child in items)
{
results.Add(new ItemModel(child));
}
return results;
}
}