Sharepoint API to create a file

priyanka konakalla 0 Reputation points

I would like to consume SharePoint API and have a question regarding that.

Will the SharePoint API to create a file always overrides the file if a file with same name already exists in the folder path

As I see overwrite =true in the below syntax

https://{site_url}/_api/web/GetFolderByServerRelativeUrl('Folder Name')/Files/add(url='a.txt',overwrite=true)

A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,249 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Ganeshkumar R 590 Reputation points

    Yes, the SharePoint REST API endpoint you mentioned will overwrite the file if a file with the same name already exists in the specified folder path. The overwrite=true parameter in the URL explicitly instructs SharePoint to replace the existing file with the new one.

    Here is the relevant part of the syntax:

    https://{site_url}/_api/web/GetFolderByServerRelativeUrl('Folder Name')/Files/add(url='a.txt',overwrite=true)


    • url='a.txt': Specifies the name of the file to be created or uploaded.
    • overwrite=true: Indicates that if a file with the same name already exists in the specified folder, it should be overwritten.


    • If overwrite=true: The existing file with the same name will be replaced by the new file.
    • If overwrite=false: An error will be returned if a file with the same name already exists.

    Preventing Overwrite:

    If you want to prevent overwriting an existing file, you can set overwrite=false. Here’s an example:

    POST https://{site_url}/_api/web/GetFolderByServerRelativeUrl('Shared Documents')/Files/add(url='example.txt',overwrite=false)
    Authorization: Bearer {access_token}
    Accept: application/json;odata=verbose
    Content-Type: application/octet-stream
    <binary file content>

    With overwrite=false, if a file named example.txt already exists in the Shared Documents folder, the request will fail, and you will need to handle that error appropriately.

    0 comments No comments

  2. Emily Du-MSFT 44,071 Reputation points Microsoft Vendor

    I have tested the Rest API https://{site_url}/_api/web/GetFolderByServerRelativeUrl('Folder Name')/Files/add(url='a.txt',overwrite=true). When you set parament overwrite=true, the newly created file will override the file with same name already exists.

    File created before:


    File created new:


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.