Tuesday, May 17, 2011

Copy file and folder recursively from local drive to SharePoint list.


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;
using System.Text.RegularExpressions;

namespace WindowsFormsApplication2
{
    public partial class Uploder : Form
    {
        public Uploder()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
         
        }

        private void button1_Click(object sender, EventArgs e)
        {
            selectFolder.ShowDialog();
            textBox1.Text = selectFolder.SelectedPath;

            using (SPSite site = new SPSite(textBox2.Text))
            {
                using (SPWeb rootWeb = site.OpenWeb())
                {
                    ActionDocLibRecursive(rootWeb);
                }
            }

        }

     



        private void ActionDocLibRecursive(SPWeb web)
        {
            // loop through each list in the web site
            foreach (SPList list in web.Lists)
            {
                // check if the list is a document library
                if (list.BaseType == SPBaseType.DocumentLibrary)
                {
                    // if it is, create a document library object
                    SPDocumentLibrary docLib = (SPDocumentLibrary)list;
                    // check that the document library is not a "catalog"
                    // (e.g. "Master Pages & Page Layouts" .. or "Web Template Gallery")
                    if (docLib.IsCatalog == false)
                    {
                        LibList.Items.Add(docLib);
                    }
                }
            }

            // call the recursive loop back on itself

            //foreach (SPWeb subWeb in web.Webs)
            //{
            //    // call resursive method on each sub-site of the current site
            //    ActionDocLibRecursive(subWeb);
            //}
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            button2.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
         
            try
            {
                selectFolder.ShowDialog();
                textBox1.Text = selectFolder.SelectedPath;
                string localPath = selectFolder.SelectedPath; //Local path.
                string sharePointSite = "http://blr-ws-168/"; //SharePoint site URL.
                string documentLibraryName = "Documents"; // SharePoint library name.
               
                // Make an object of your SharePoint site.
                using (SPSite oSite = new SPSite(sharePointSite))
                {
                    SPWeb oWeb = oSite.OpenWeb();
                    ActionDocLibRecursive(oWeb);

                    CreateDirectories(localPath,oWeb.Folders[documentLibraryName].SubFolders);
                }
            }
            catch (Exception ex)
            {
               
               // MessageBox.Show("Error:" + ex.Message);
               // richTextBox1.AppendText( ex.Message + "\n");
            }
        }

        private void CreateDirectories(string path, SPFolderCollection oFolderCollection)
        {
            //Upload Multiple Files
            foreach (FileInfo oFI in new DirectoryInfo(path).GetFiles())
            {
                CreateValidFileName(oFI.Name);
                FileStream fileStream = File.OpenRead(oFI.FullName);
                SPFile spfile = oFolderCollection.Folder.Files.Add
                            (oFI.Name, fileStream, true);
           
                label4.Text = oFI.Name;
               
                spfile.Update();
                //richTextBox1.AppendText(oFI.Name + "Was Added Sucessfully\n"+ " In "+ spfile);
               
            }

            //Upload Multiple Folders
            foreach (DirectoryInfo oDI in new DirectoryInfo(path).GetDirectories())
            {
                string sFolderName = oDI.FullName.Split('\\')[oDI.FullName.Split('\\').Length - 2];
                SPFolder spNewFolder = oFolderCollection.Add(sFolderName);
               
                spNewFolder.Update();
                //Recursive call to create child folder
                CreateDirectories(oDI.FullName, spNewFolder.SubFolders);

            }

        }

        public void CreateValidFileName(string Filename)
        {
            string invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars()));
            string invalidReStr = string.Format(@"[{0}]+", invalidChars);
            Filename = Regex.Replace(Filename, invalidReStr, "_");
           
        }

       
    }
}

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

#Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc

 #Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc Linux is an open-source operating system that is loved by millio...