Introduction

The aim of this project is to demonstrate how programmatically we can download and upload file to sharepoint. Using SharePoint Object model, this task can be easily achieved. Other option is using SharePoint List webservice.

This project is a windows form application. Opening solution in Visual Studio 2008. Make sure SharePoint dll's are present of the machine.

For screen show

  1. Enter SP Site URL where document library is available.
  2. Enter Document Library
  3. Get Files button click will fill the Files combobox with list of files available on the document library
  4. Download File button will download the selected file. This will opens up Save File dialog, select path.
  5. Select File button will open up Open File Dialog. Select the file to upload
  6. Upload button will upload the file

I haven't handled versioned documents.

Iterate through file collection and find the file to download. Once we have the file, use OpenBinary() method will return byte[] of file content. Then we will use FileStream class along with BinaryWriter for writing the file content to the file to be saved.

C#
Edit|Remove
using (SPSite site = new SPSite(txtSite.Text)) 
                { 
                    using (SPWeb web = site.OpenWeb()) 
                    { 
                        SPFolder myLibrary = web.Folders[txtDocLib.Text]; 
 
                        foreach (SPFile file in myLibrary.Files) 
                        { 
                            if (file.Name == comboBox1.SelectedItem.ToString()) 
                            { 
                                byte[] bytes = file.OpenBinary(); 
 
                                try 
                                { 
                                    FileStream fs = new FileStream(dialog.FileName, FileMode.Create, FileAccess.ReadWrite); 
                                    BinaryWriter bw = new BinaryWriter(fs); 
                                    bw.Write(bytes); 
                                    bw.Close(); 
                                    MessageBox.Show("File downloaded to: " + dialog.FileName); 
                                } 
                                catch (Exception ex) 
                                { 
                                    MessageBox.Show(ex.Message); 
                                } 
 
                            }                             
                        } 
                    } 
                } 
 

 

C#
Edit|Remove
using (SPSite oSite = new SPSite(txtSite.Text)) 
            { 
                using (SPWeb oWeb = oSite.OpenWeb()) 
                {                     
                    SPFolder myLibrary = oWeb.Folders[txtDocLib.Text]; 
 
                    // Prepare to upload 
                    Boolean replaceExistingFiles = true; 
                    String fileName = System.IO.Path.GetFileName(txtSelectedFile.Text); 
                    FileStream fileStream = File.OpenRead(txtSelectedFile.Text); 
 
                    // Upload document 
                    SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles); 
 
                    // Commit  
                    myLibrary.Update(); 
                } 
            } 
 
 Uploading file is easier. We will use the SPFolder.Files.Add method

 

Source Code Files