0
votes

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:

  1. 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.
  2. 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;
    }
}
1

1 Answers

0
votes

this is possible using OData query options.

1) Expand is exactly the right terminology for pulling in related resources in the same response body.

The request you described can be accomplished as the following in REST: https://graph.microsoft.com/v1.0/me/drive/special/photos/children?$select=id,name&$expand=thumbnails

$select specifies that you want just those properties in the response body, and $expand pulls in the associated thumbnails collection for each drive item.

2) You can add an additional $orderby query option to specify the sort order. All together, it looks as follows:

https://graph.microsoft.com/v1.0/me/drive/special/photos/children?$select=id,name&$expand=thumbnails&$orderby=createdDateTime desc

I believe you can pass each of these query options as strings to OrderBy, Expand and Select as "createdDateTime desc", "thumbnails" and "id, name".