WebHosting

Showing posts with label recursive copy file from local drive to sharepoint list. Show all posts
Showing posts with label recursive copy file from local drive to sharepoint list. Show all posts

Tuesday, May 17, 2011

Copy listitems from one custom list to another, then move them into subfolders.


SPListItem listItem = web.GetListItem(listItemUrl);
listItem.CopyTo(destinationUrl); // Copies the item to the specified destinations
or
listItem.CopyFrom(sourceUrl); // Overwrites the current item with the specified version of the item.

SPListItem item = Web.Lists["Announcement"].GetItemById(5);
SPFile file = Web.GetFile(item.Url);
file.MoveTo("New location...with the ID_.000");


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, "_");
           
        }

       
    }
}

Featured Posts

How to Enable and Configure Remote Desktop (RDP) on Ubuntu 24.04.4 LTS

Remote Desktop Protocol (RDP) support in Ubuntu has become considerably easier with the GNOME Remote Desktop stack included in modern Ubuntu...