如何使用 MS GRAPH SDK C# 获取驱动器信息(ParentReference)以将文件复制到另一个驱动器

How to get Drive infos(ParentReference) to copy file to another drive using MS GRAPH SDK C#

我正在尝试使用 C# 中的 MS GRAPH SDK 将文件从一个驱动器复制到另一个驱动器。

我可以通过使用硬编码的 parentReference(DriveId 和 Id)成功地做到这一点,因为我通过从目标驱动器中的现有文件中获取 parentReference 来检索它。

"/sites/SiteId/drives/DriveId/root/children"

        "parentReference": {
            "driveId": "b!07UG1YX6EEWI-xYElkDOj9a5a_hmu6RDt0mpVQfH3RFpCR1wxODCRpss4Xq4g75t",
            "driveType": "documentLibrary",
            "id": "01QVACJXF6Y2GOVW7725BZO354PWSELRRZ",
            "path": "/drives/b!07UG1YX6EEWI-xYElkDOj9a5a_hmu6RDt0mpVQfH3RFpCR1wxODCRpss4Xq4g75t/root:"
        },

我当前的代码:

        var parentReference = new ItemReference
        {
            DriveId = destination.Id, //retrieved DriveId previously
            Id = "01QVACJXF6Y2GOVW7725BZO354PWSELRRZ"
        };

        var name = DocumentTitle + "."+ extensionTemplate;

        var result = await graphClient.Sites[IdGestDoc].Drives[templateDrive.Id].Items[template.DriveItem.Id]
            .Copy(name, parentReference)
            .Request()
            .PostAsync();

但我的问题是当驱动器中没有现有文件时如何获取此信息?

我可以检索 DriveId 但不能检索 parentReference 的 ID..

任何帮助将不胜感激!

事实上 Copy a DriveItem endpoint 期望 parentReference 参数为:

Reference to the parent item the copy will be created in.

意思是,要将文件复制到空驱动器中,需要指定目标驱动器ID该驱动器的根文件夹,像这样:

var parentReference = new ItemReference
{
     DriveId = "--target drive-id-goes-here--",
     Id = "--root folder-of-drive-goes-here--"
};


 var result = await graphClient.Sites[siteId].Drives[driveId].Items[itemId]
 .Copy(name, parentReference)
 .Request()
 .PostAsync();

其中驱动器 ID 及其 根文件夹 ID 可以像这样预先确定:

GET https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{target-drive-id}/root?select=id,parentReference


{
    "id": "--root-folder-id-of-drive",
    "parentReference": {
        "driveId": "--drive-id--",
        "driveType": "documentLibrary"
    }
}