Search code examples
microsoft-graph-apionedrive

How do I make OneDrive put fail, when the item already exists?


Using the Microsoft Graph Explorer, I can use http PUT to create a OneDrive item. For example, the uri below creates a testfile.txt at my root:

https://graph.microsoft.com/v1.0/me/drive/root:/testfile.txt:/content

Now, I would like the PUT to fail, if the file already exists, and the general documentation on OneDrive items, https://dev.onedrive.com/items/upload_put.htm#optional-query-string-parameters, states how to specify conflict behavior - if I read it correctly, like:

https://graph.microsoft.com/v1.0/me/drive/root:/testfile.txt:/content?@microsoft.graph.conflictBehavior=fail

However, the PUT still successfully overwrites the existing 'testfile.txt'.

Can anyone help me write a PUT query so that it fails in case the item already exists? Is that even supported by OneDrive? Do I need to go to the beta version of Microsoft Graph?


Solution

  • Per the documentation this should be specified within request body, not as a query parameter:

    {
        "item": {
            "@microsoft.graph.conflictBehavior": "fail"
        }
    }
    

    This is also only supported in the Resumable Upload" scenario as the request body for a direct upload is the file itself. In general, the guidance is to always use resumable uploads where possible. Direct uploads are limited to files less than 4MB which makes it unsuitable for most scenarios.

    It is also worth noting that the documentation you referenced is for the standalone OneDrive API. While this is very similar to Graph's implementation, they are not entirely interchangeable. The Microsoft Graph documentation is the only authoritative source for Graph information.