Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent overwrite of Upload File via Microsoft Graph API

I'm using following code through the Microsoft Graph API dot.net sdk

using (Stream fileStream = file.InputStream)
            {
                DriveItem uploadedFile = await graphClient
                    .Drives[DRIVE_ID]
                     .Root.ItemWithPath($"{root}{relative}{file.FileName}")
                        .Content.Request()
                        .PutAsync<DriveItem>(fileStream);


            }

to upload a simple file to OneDrive. Is it possible to prevent the file upload if the file already exists?

Update

The Microsoft Graph Documentation has been updated here a snippet from it:

Request body

https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/item_createuploadsession

No request body is required. However, you can specify a request body to provide additional data about the file being uploaded. For example, to control the behavior if the filename is already taken, you can specify the conflict behavior property in the body of the request.

{
    "item": {
        "@microsoft.graph.conflictBehavior": "rename"
    }
}
like image 910
Alx Avatar asked Mar 10 '23 09:03

Alx


1 Answers

Unfortunately no. In order to prevent file override (which also creates a new version) you have to first check if the file with the path already exists. You can do that programmatically by listing parent folder contents and checking by filename.

like image 141
spery Avatar answered Mar 29 '23 07:03

spery