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

标签 c# microsoft-graph-api sharepoint-online microsoft-graph-sdks

我正在尝试使用 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"
    }
}

关于c# - 如何使用 MS GRAPH SDK C# 获取驱动器信息(ParentReference) 将文件复制到另一个驱动器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59825380/

相关文章:

arrays - 使用数组将 SharePoint Online 列表转换为 JSON

powershell - 如何使用 PowerShell 从 Sharepoint Online 列表中获取项目?

c# - Entity Framework Core 添加迁移失败

javascript - 使用 Graph OAuth v2.0 刷新 token

api - Office 365 REST Api和Microsoft Graph之间的区别

azure - Microsoft GRAPH API 可以在没有 Azure 应用程序注册的情况下使用吗?

sharepoint - 如何在 Microsoft Graph API 中使用过滤器来获取 SharePoint 项目?

c# - 编码的 UI 测试_C#_manual_intervention

c# - 如何使用 HttpClient 根据个人请求设置 HttpHeader

c# - 到飞机的距离