Sharepoint 2007 File Download/Upload Demo

IntroductionThe 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

C# (487.8 KB)
 
 
 
 
 
4 Star
(2)
2,256 times
Add to favorites
5/12/2011
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using System.IO;

namespace SharePointDownloadUploadDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnGetFiles_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtSite.Text))
                {
                    MessageBox.Show("Enter Site URL");
                    return;
                }
                if (string.IsNullOrEmpty(txtDocLib.Text))
                {
                    MessageBox.Show("Enter Document Library Name");
                    return;
                }

                using (SPSite site = new SPSite(txtSite.Text))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPFolder myLibrary = web.Folders[txtDocLib.Text];

                        foreach (SPFile file in myLibrary.Files)
                        {
                            comboBox1.Items.Add(file.Name);
                        }                        
                    }
                }
            }
            catch
            {
                MessageBox.Show("Some Error Occured");
            }
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            if (comboBox1.Items.Count > 0 && comboBox1.SelectedIndex != -1)
            {
                SaveFileDialog dialog = new SaveFileDialog();
                dialog.ShowDialog();

                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);
                                }

                            }                            
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("Select file to download");
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.ShowDialog();
            txtSelectedFile.Text = dialog.FileName;
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            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();
                }
            }

        }
    }
}