我正在尝试使用Microsoft图形API从OneDrive获取最新照片的缩略图。
我一直在GitHub上使用Microsoft图形OneDrive照片浏览器示例作为指南,我正在尝试修改它以仅显示最新的照片。
我需要两件事的帮助:
- 展开子文件夹。我不确定这是否是正确的术语但是如果我提出这样的要求(https://graph.microsoft.com/v1.0/me/drive/special/photos/children?select=id,name(它将返回文件夹的集合。我想要回应包含照片缩略图的集合,而不必制作每个文件夹的另一个请求以获取这些缩略图。
- 按创建日期降序对请求进行排序。
下面是照片浏览器示例应用中的代码。
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;
}
}
可以使用 OData 查询选项来实现这一点。
1(扩展是在同一响应正文中拉入相关资源的正确术语。
您描述的请求可以在 REST 中按如下方式完成: https://graph.microsoft.com/v1.0/me/drive/special/photos/children?$select=id,name&$expand=thumbnails
$select指定响应正文中只需要这些属性,$expand拉取每个驱动器项的关联缩略图集合。
2( 您可以添加额外的$orderby查询选项来指定排序顺序。总之,它看起来如下:
https://graph.microsoft.com/v1.0/me/drive/special/photos/children?$select=id,name&$expand=thumbnails&$orderby=createdDateTime desc
我相信您可以将这些查询选项中的每一个作为字符串传递给 OrderBy、展开和选择作为"createdDateTime desc"、"缩略图"和"id、name"。