1

I use the below code

 FileCreationInformation newFile = new FileCreationInformation();
            newFile.Content = System.IO.File.ReadAllBytes(filePath);
            newFile.Url = fileName;
 Folder folder = web.GetFolderByServerRelativeUrl(folderName);
                uploadFile = folder.Files.Add(newFile);

This works fine when the file is already not present, but throws error when file exists already

How can I update the file instead of adding if its existing?

2

You can overwrite file if it already exists by setting Overwrite equal true.

 newFile.Overwrite = true;

Find more in MSDN FileCreationInformation.Overwrite

Modified Code

FileCreationInformation newFile = new FileCreationInformation();
            newFile.Content = System.IO.File.ReadAllBytes(filePath);
            newFile.Url = fileName;
            newFile.Overwrite = true;
 Folder folder = web.GetFolderByServerRelativeUrl(folderName);
                uploadFile = folder.Files.Add(newFile);

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.