views:

9081

answers:

5

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.

+7  A: 

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);
                }
            }
        }
Henrique Zacchi
Note that this code leaks the new SPSite. You should never chain new SPSite(url).OpenWeb().
dahlbyk
Nice catch. I've edited the code to dispose the SPWeb.I've seen some blogs asserting that if you call SPSite.Dispose, it will automatically call Dispose on all the contained SPWebs (not the case in WSS 2).Anyway, I'll stick to the MSDN recommendation which is to dispose all the SPWebs.
Henrique Zacchi
I never did anything with Sharepoint. What do I have to install and set a reference to to be able to define a variable of type "SPSite?"I had some success copying a file to a sharepoint site by programmatically creating a Web Folder and saving to it, but I run into issues when the word doc has missing sharepoint properties
Velika
Chadworthington, SPSite is part of Microsoft.SharePoint namespace, so you need to add reference to Microsoft.SharePoint.dll. Assuming you are developing on the Server, the dll can be found here: C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\Microsoft.SharePoint.dll
Henrique Zacchi
+2  A: 

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.

Paul-Jan
+1  A: 

Hello:

Can u please help me on this... How to upload a document to a sharepoint document library using javascript ??

You should better ask this as a new question, not post it here as an answer. More people would look at it that way. The "Ask Question" button is in the top right of the page...
sth
+1  A: 

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.

CMD
+1  A: 

How to pass authentication login details to sharepoint before upload a file. The above script doesnt work because of the authentication issue.

Raana