Search This Blog

Monday, December 25, 2017

Move File - Directory Folder to SharePoint Document Library

using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace FileMover
{
   static class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://win-2016"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    string dirPath = string.Empty;
                    byte[] byteArr = null;
                    string[] dirs = Directory.GetDirectories(@"D:\TestingFolder", "*.*", SearchOption.AllDirectories);
                    foreach (string dir in dirs)
                    {
                        dirPath = Regex.Replace(dir.Split(':')[1], @"\\", "/");
                        Console.WriteLine(dirPath);
                        var folder = web.CreateFolder("testDocument", dirPath);
                        DirectoryInfo Dinfo = new DirectoryInfo(dir);
                        FileInfo[] Finfo = Dinfo.GetFiles("*.*");
                        foreach (FileInfo f in Finfo)
                        {                         
                            Console.WriteLine(f.Name);
                            byteArr = File.ReadAllBytes(f.FullName);
                            web.Files.Add("testDocument" + dirPath + "/" + f.Name, byteArr, true);
                        }
                    }
                }
            }
            Console.ReadKey();
        }
        /// <summary>
        /// Ensure SPFolder
        /// </summary>
        /// <param name="web"></param>
        /// <param name="listTitle"></param>
        /// <param name="folderUrl"></param>
        /// <returns></returns>
        public static SPFolder CreateFolder(this SPWeb web, string listTitle, string folderUrl)
        {
            if (string.IsNullOrEmpty(folderUrl))
                throw new ArgumentNullException("folderUrl");
            var list = web.Lists.TryGetList(listTitle);
            return CreateFolderInternal(list, list.RootFolder, folderUrl);
        }


        private static SPFolder CreateFolderInternal(SPList list, SPFolder parentFolder, string folderUrl)
        {
            var folderNames = folderUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            var folderName = folderNames[0];

            var curFolder =
                parentFolder.SubFolders.Cast<SPFolder>()
                            .FirstOrDefault(
                                f =>
                                System.String.Compare(f.Name, folderName, System.StringComparison.OrdinalIgnoreCase) ==
                                0);
            if (curFolder == null)
            {
                var folderItem = list.Items.Add(parentFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder,
                                                folderName);
                folderItem.SystemUpdate();
                curFolder = folderItem.Folder;
            }


            if (folderNames.Length > 1)
            {
                var subFolderUrl = string.Join("/", folderNames, 1, folderNames.Length - 1);
                return CreateFolderInternal(list, curFolder, subFolderUrl);
            }
            return curFolder;
        }
    }

}

No comments:

Post a Comment