Search code examples
microsoft-graph-apionedrive

Do not replace existing file when uploading file with same name Microsoft Graph API


How can I prevent replacing the existing file with a new file which has the same name, when I upload file to one drive? I am using PUT /me/drive/items/{parent-id}:/{filename}:/content docs end point. I instead need to keep indexing (test.jpg, test (1).jpg) or, just like google drive does, add two files with the same name. enter image description here


Solution

  • You can control this behavior using Instance Attributes, specifically the @microsoft.graph.conflictBehavior query parameter. There are three supported conflict behaviors; fail, replace (the default), and rename.

    The conflict resolution behavior for actions that create a new item. You can use the values fail, replace, or rename. The default for PUT is replace. An item will never be returned with this annotation. Write-only.

    In order to have it automatically rename the file, you add @microsoft.graph.conflictBehavior=rename as a query parameter to your URI.

    PUT /me/drive/items/{parent-id}:/{filename}:/content?@microsoft.graph.conflictBehavior=rename