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.
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.
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))
{
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();
}
else
{
throw new FileNotFoundException("File not found.", fileToUpload);
}
}
}
As an alternative to the webservices, you can use the put document call from the FrontPage RPC API. This has the additional benefit of enabling you to provide meta-data (columns) in the same request as the file data. The obvious drawback is that the protocol is a bit more obscure (compared to the very well documented webservices).
For a reference application that explains the use of Frontpage RPC, see the SharePad project on CodePlex.
Hello:
Can u please help me on this... How to upload a document to a sharepoint document library using javascript ??
Is there a way to check if the document already exists in the library? I'd hate to loop through all the items or catch the file already exists error.
How to pass authentication login details to sharepoint before upload a file. The above script doesnt work because of the authentication issue.