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)
Parameters:
- 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.
Behavior:
- 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.