我正在尝试使用Microsoft Graph API从OneDrive获取最新照片的缩略图。
我一直在GitHub上使用Microsoft Graph OneDrive Photo Browser示例作为指南,我试图修改它以仅显示最新照片。
我需要帮助两件事:
以下是照片浏览器示例应用中的代码。
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;
}
}
答案 0 :(得分:0)
可以使用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”。