Implementing Multi-Threading in SharePoint To Upload Files using CSOM C# – An Insight

Sathish Nadarajan
 
Solution Architect
November 21, 2016
 
Rate this article
 
Views
11441

Recently, I was involved in creation of a tool (EXE) which needs to upload a bunch of files to SharePoint Online Site Collection. Somehow, we can relate that as a kind of Migration. From the Physical drive, I need to upload to a Document Library.

In this requirement, the performance was the most critical factor. Hence, I thought of implementing the Multi-Threading using Client Side Object Model. And thought of sharing with the Community.

The piece of code is straight forward using the namespace System.Threading.Tasks

The below sample code will upload a set of files to SharePoint Document Library in Multi Threaded approach.

 namespace Console.Office365
 {
     using Microsoft.SharePoint.Client;
     using Newtonsoft.Json.Linq;
     using System;
     using System.Collections.Generic;
     using System.IO;
     using System.Linq;
     using System.Threading.Tasks;
 
     class Program
     {
         static void Main(string[] args)
         {
             UploadFilesMultiThread();
         }
 
 
 
         public static void UploadFilesMultiThread()
         {
             string[] filePaths = System.IO.Directory.GetFiles("D:\Temp\UploadFiles");
 
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "https://*********.sharepoint.com/sites/CommunitySite/";
             string userName = "Sathish@*****.onmicrosoft.com";
             string password = "*************";
 
             
 
             var tasks = new List<Task>();
 
             tasks.AddRange(filePaths.Select(filePath =>
             {
                 return Task.Factory.StartNew(() =>
                 {
                     try
                     {
                         using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
                         {
                             Web web = ctx.Web;
                             ctx.Load(web);
 
                             List list = web.Lists.GetByTitle("MyDocumentsLibrary");
                             ctx.Load(list);
                             ctx.Load(list.RootFolder);
 
                             ctx.ExecuteQueryRetry();
 
                             if (System.IO.File.Exists(filePath))
                             {
                                 System.Threading.Thread.Sleep(10000);
 
                                 System.Console.ForegroundColor = ConsoleColor.Green;
                                 System.Console.WriteLine("Entered into : " + Path.GetFileName(filePath));
 
 
                                 Folder folder = web.GetFolderByServerRelativeUrl(list.RootFolder.ServerRelativeUrl);
                                 folder.UploadFile(Path.GetFileName(filePath), filePath, true);
 
                                 folder.Update();
 
                                 ctx.Load(folder);
                                 ctx.ExecuteQueryRetry();
 
                             }
                         }
                     }
                     catch (Exception ex)
                     {
                         System.Console.ForegroundColor = ConsoleColor.Red;
                         System.Console.WriteLine(ex.Message);
                     }
                 }, TaskCreationOptions.LongRunning);
             }));
 
 
             Task.WaitAll(tasks.ToArray());
 
             System.Console.WriteLine("Process Completed");
             System.Console.ReadLine();
         }
     }
 
 }
 

Hope this helps.

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment