Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello ,

I'd like to upload a file dto document library but if there is a file
already exists with the same name I'd like to update the existing one
with the one i am trying to upload to a Sharepoint 2013 document library .

Is there anyone can tell me how to do it :(
Posted
Updated 18-Oct-20 14:59pm

Anyway I solved my own problem,
In case of someone else looks for same thing Here is my solution .

private static void uploadDocument(string DocumentLibraryString, Uri SiteUrl, string DocumenttoUploadUrl)
{
using (SPSite Site = new SPSite(SiteUrl.ToString()))
{
using (SPWeb Web = Site.OpenWeb())
{
if (!System.IO.File.Exists(DocumenttoUploadUrl))
throw new FileNotFoundException("File not found.", DocumenttoUploadUrl);

SPFolder myLibrary = Web.GetList(DocumentLibraryString).RootFolder;

// Prepare to upload
Boolean replaceExistingFiles = true;
String fileName = System.IO.Path.GetFileName(DocumenttoUploadUrl);
FileStream fileStream = File.OpenRead(DocumenttoUploadUrl);

// Upload document
SPFile spFile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);

myLibrary.Update();
if (myLibrary.RequiresCheckout)
{
spFile.CheckIn("Automatically created file checked in", SPCheckinType.MajorCheckIn);
spFile.Publish("Automatically created file published");
}

}
}

}
 
Share this answer
 
Thanks for sharing!
What program/language did you use?
 
Share this answer
 
Comments
CHill60 19-Oct-20 4:42am    
If you wish to comment on an answer then use the "Have a Question or Comment?" link next to it. This is not a solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900