Bulk Import Folders and Files in SharePoint Document Library | Part 1

This noon I was on a call with SharePoint Development team to discuss few activities which is pretty straight forward approach for SharePoint IT Professionals. However, we need to inform about the activity we carry out in the farm to both IT Pro’s and developers. By doing this we simply avoid confusion. And, as always in any farm bulk upload of files and folders may cause one or other issues like threshold, performance high risk of uploading vulnerable files etc.

Our client came with 5000 odd folders which has almost 9000 files approximately and the requirement is to push it to document folder. For example, files underneath the folder should be organized as a single folder and files needs to be stored in it. The appended a request “Share the cmdlet so we can add more in future!”

Everything is easy if we build one! There is no out of the box solution. We can’t drag and drop easily as we know the limitations in document library. So, I built a small cmdlet in Visual C# which serves the purpose.

The folder structure is very simple, Main Folder which contains N number of folder and each folder may have some files. No Folders underneath the folders.

using System;
using System.IO;
using System.Management.Automation;
using Microsoft.SharePoint.Client;
namespace SP2010Admin
{
    [Cmdlet(VerbsData.Import, "Document")]
    public class ImportDocument : Cmdlet
    {
        [Parameter(Mandatory = true)]
        public Uri SPUrl;
        [Parameter(Mandatory = true)]
        public string SPDocLib;
        [Parameter(Mandatory = true,
                   ValueFromPipeline = true,
                   ValueFromPipelineByPropertyName = true)]
        public string FolderPath;
        protected override void ProcessRecord()
        {
            ClientContext ctx = new ClientContext(SPUrl);
            Web web = ctx.Web;
            ctx.Load(web);
            ctx.ExecuteQuery();

            List DocLib = web.Lists.GetByTitle(SPDocLib);
            string[] filenames = Directory.GetFiles(FolderPath);
            DirectoryInfo dInfo = new DirectoryInfo(FolderPath);
            FolderCollection folders = DocLib.RootFolder.Folders;
            ctx.Load(folders);
            ctx.ExecuteQuery();
            bool exists = false;
            foreach (Folder efolder in folders)
            {
                if (efolder.Name.Equals(dInfo.Name))
                {
                    foreach (string filename in filenames)
                    {
                        string[] names = filename.Split('\\');
                        WriteObject("Existing " + filename);
                        byte[] data = System.IO.File.ReadAllBytes(filename);
                        FileCreationInformation fci = new FileCreationInformation();
                        fci.Content = data;
                        fci.Url = names[names.Length - 1];
                        efolder.Files.Add(fci);
                        exists = true;
                    }
                }
            }
            if (!exists)
            {
                Folder tFolder = folders.Add(dInfo.Name);
                foreach (string filename in filenames)
                {
                    string[] names = filename.Split('\\');
                    WriteObject("Uploading file " + filename);
                    byte[] data = System.IO.File.ReadAllBytes(filename);
                    FileCreationInformation fci = new FileCreationInformation();
                    fci.Content = data;
                    fci.Url = names[names.Length - 1];
                    tFolder.Files.Add(fci);
                    exists = true;
                }
                ctx.ExecuteQuery();
            }
        }
    }
}

Usage:

Import-Module "c:\users\ChenV\documents\visual studio 2015\Projects\SP2010Admin\SP2010Admin\bin\Debug\SP2010Admin.dll" -Verbose
(Get-ChildItem "C:\Users\ChenV\ProjectFolder\Test\" -Recurse).FullName | Import-Document -SPUrl http://www.contoso.com/web -SPDocLib "Doc Lib" -Verbose

In Part 2 we will discuss about the best practice and will enhance this code much better! Enjoy PowerShell 🙂 🙂 🙂

Thanks to Author Rashu, Rahul in TechNet Wiki

Leave a Reply

Your email address will not be published. Required fields are marked *