我正在尝试使用Microsoft Graph API从OneDrive获取最新照片的缩略图 .
我一直在GitHub上使用Microsoft Graph OneDrive Photo Browser示例作为指南,我正在尝试修改它以仅显示最新的照片 .
我需要两件事的帮助:
-
展开子文件夹 . 我不确定这是否是正确的术语,但如果我提出这样的请求(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;
}
}
1 回答
这可以使用OData query options .
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,Expand和Select作为“createdDateTime desc”,“缩略图”和“id,name” .