I am trying to upload a file using REST API as below:

String.format("{0}/_api/web/getfolderbyserverrelativeurl('{1}')/files" +"/add(overwrite=true, url='{2}')",serverUrl, serverRelativeUrlToFolder, fileName);

But if a file already exists, it doe not replace the existing file, instead throws error "A file with an equivalent name exists."

Please advice me how to proceed with replacing the file.

  • Are you trying to update the file in document library or list item attachment? – Ganesh Sanap Dec 3 '20 at 17:49
  • Inside document library – Kailash Sahoo Dec 3 '20 at 17:50
  • As per the Microsoft documentation, you can update a file by using the PUT method. give it a try and let me know if it works for you. – Ganesh Sanap Dec 3 '20 at 17:55
  • Same "File existing" error using PUT – Kailash Sahoo Dec 3 '20 at 18:23

I think you are using the code in this article to upload a file: https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/upload-a-file-by-using-the-rest-api-and-jquery

In fact, the code will upload the file then change the file name after uploading. When uploading, it will replace the exisiting file if a file already exists.

However,if you try to change the name of the file with existing files'. You will get this error.

For example:

I have a file with name aa.xlsx in the library:

enter image description here

I upload a file test.xlsx and try to rename it with aa.xlsx. I would get this error. enter image description here

Infact, the file test.xlsx is uploaded successfully, but we cannot changed it name with aa.xlsx. enter image description here

As a workaround, you could rename the file (test.xlsx) in the local machine (aa.xlsx). Then upload it. Just do not enter the name for the file.

enter image description here

It replaced the existing aa.xlsx. enter image description here

  • Thanks for the headache! I am uploading an existing file with exactly same filename with updated content. the concern is I am setting the "overwrite=true" within the REST API URL, but it is throwing error instead of replacing. The Same API works if I try to upload a new file. – Kailash Sahoo Dec 9 '20 at 8:03

I have found the same problem with Inuktituk (Unified Canadian Aboriginal Syllabics Unicode block) even though the two files have different names with different lengths and different number of characters. Adding some ascii character to the beginning or end of the file name resolves it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.