SharePoint Pals
 | Sign In
Implementing Multi-Threading in SharePoint To Upload Files using CSOM C# – An Insight
by Sathish Nadarajan 21 Nov 2016
SharePoint MVP
Today  :   26     Total  :    1306

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)
         public static void UploadFilesMultiThread()
             string[] filePaths = System.IO.Directory.GetFiles("D:\\Temp\\UploadFiles");
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
             string siteUrl = "https://*********";
             string userName = "Sathish@*****";
             string password = "*************";
             var tasks = new List<Task>();
             tasks.AddRange(filePaths.Select(filePath =>
                 return Task.Factory.StartNew(() =>
                         using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
                             Web web = ctx.Web;
                             List list = web.Lists.GetByTitle("MyDocumentsLibrary");
                             if (System.IO.File.Exists(filePath))
                                 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);
                     catch (Exception ex)
                         System.Console.ForegroundColor = ConsoleColor.Red;
                 }, TaskCreationOptions.LongRunning);
             System.Console.WriteLine("Process Completed");

Hope this helps.

Happy Coding,

Sathish Nadarajan.

blog comments powered by Disqus

SharePoint Pals

SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile


Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community

SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to in a word document.

Related Resources

Recent Tweets

Twitter November 21, 21:08
Implementing Multi-Threading with MaxDegreeOfParallelism in SharePoint using CSOM – An Insight -

Twitter November 21, 12:48
Configure Windows Firewall for SQL Server Analysis Service (SSAS) -

Twitter November 21, 12:46
Implementing Multi-Threading in SharePoint To Upload Files using CSOM C# – An Insight -

Twitter November 20, 21:48
How to Get the Client Context Using App Access token, by passing Client ID using CSOM in SharePoint Office 365 -

Twitter November 18, 21:30
How to Enable Auto Approval For Permission Requests in Community Site Template – Office 2013 using Client Side Object Model (CSOM) C#

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation