一个基于查询的方法可能会在这方面加以考虑。由于设计DriveItem.name
属性在文件夹中是唯一的,因此以下查询演示了如何driveItem
按名称过滤以确定驱动器项是否存在:
https://graph.microsoft.com/v1.0/me/drive/items/{parent-item-id}/children?$filter=name eq '{folder-name}'
可以用C#表示如下:
var items = await graphClient
.Me
.Drive
.Items[parentFolderId]
.Children
.Request()
.Filter($"name eq '{folderName}'")
.GetAsync();
给定提供的端点,流程可以包括以下步骤:
- 提交请求以确定是否存在具有给定名称的文件夹
- 如果找不到文件夹,请提交第二个文件夹(或返回现有文件夹)
例
这是一个更新的示例
//1.ensure drive item already exists (filtering by name)
var items = await graphClient
.Me
.Drive
.Items[parentFolderId]
.Children
.Request()
.Filter($"name eq '{folderName}'")
.GetAsync();
if (items.Count > 0) //found existing item (folder facet)
{
Console.WriteLine(items[0].Id); //<- gives an existing DriveItem Id (folder facet)
}
else
{
//2. create a folder facet
var driveItem = new DriveItem
{
Name = folderName,
Folder = new Folder
{
},
AdditionalData = new Dictionary<string, object>()
{
{"@microsoft.graph.conflictBehavior","rename"}
}
};
var newFolder = await graphClient
.Me
.Drive
.Items[parentFolderId]
.Children
.Request()
.AddAsync(driveItem);
}