简体   繁体   中英

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

I'm trying to copy a file from a drive to another using MS GRAPH SDK in C#.

I can successfuly do this by using hardcoded parentReference (DriveId & Id), because i retrieved this by getting the parentReference from an existing file in the destination drive.

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

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

My current code:

        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();

But my question is how to get this informations when there is no existing files in the drive?

I can retrieve the DriveId but not the Id for the parentReference..

Any help will be appreciated !

In fact Copy a DriveItem endpoint expects parentReference parameter to be:

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

meaning, to copy a file into an empty drive, target drive id and root folder of this drive needs to be specified, like this:

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();

where drive id and its root folder id could be determined beforehand like this:

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"
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM