Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you upload a file to a document library in sharepoint?

How do you programmatically upload a file to a document library in sharepoint?

I am currently making a Windows application using C# that will add documents to a document library list.

like image 815
Adyt Avatar asked Jan 22 '09 08:01

Adyt


People also ask

Why can't I upload documents to SharePoint?

In SharePoint online, avoid uploading files larger than 15 GB, which is the maximum default file size. You can try dragging and dropping the files to the space in the SharePoint library where it says drag files here. The library should display "Drop here" when you hover the file over it.

How do I upload a file from power apps to a SharePoint library?

Make A Flow To Upload Documents To A SharePoint LibraryOpen the Power Automate action from the top menu and select Create a new flow. Choose the Power Apps button template. Name the flow UploadFileToDocument library and click Save.


2 Answers

You can upload documents to SharePoint libraries using the Object Model or SharePoint Webservices.

Upload using Object Model:

String fileToUpload = @"C:\YourFile.txt"; String sharePointSite = "http://yoursite.com/sites/Research/"; String documentLibraryName = "Shared Documents";  using (SPSite oSite = new SPSite(sharePointSite)) {     using (SPWeb oWeb = oSite.OpenWeb())     {         if (!System.IO.File.Exists(fileToUpload))             throw new FileNotFoundException("File not found.", fileToUpload);                              SPFolder myLibrary = oWeb.Folders[documentLibraryName];          // Prepare to upload         Boolean replaceExistingFiles = true;         String fileName = System.IO.Path.GetFileName(fileToUpload);         FileStream fileStream = File.OpenRead(fileToUpload);          // Upload document         SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);          // Commit          myLibrary.Update();     } } 
like image 61
Henrique Zacchi Avatar answered Oct 12 '22 01:10

Henrique Zacchi


if you get this error "Value does not fall within the expected range" in this line:

SPFolder myLibrary = oWeb.Folders[documentLibraryName]; 

use instead this to fix the error:

SPFolder myLibrary = oWeb.GetList(URL OR NAME).RootFolder; 

Use always URl to get Lists or others because they are unique, names are not the best way ;)

like image 23
Ricardo Vieira Avatar answered Oct 12 '22 00:10

Ricardo Vieira