Implementing Multi-Threading With MaxDegreeOfParallelism For Uploading

In this blog, we will see how to use Multi-Threading to upload a bunch of files in SharePoint using CSOM. From the local path, we need to upload files to our document library. Using multi-threading we are simultaneously uploading files in batches, we may face, the request time out, forbidden exceptions, etc. To avoid such issues, use MaxDegreeOfParallelism Parameter. MaxDegreeOfParallelism Parameter is the one, which decides how many items in the array are to be taken at a time simultaneously.

These are the below code we need to implement,

using System;
using System.Collections.Generic;
using Microsoft.SharePoint.Client;
using System.IO;
using OfficeDevPnP.Core;
using System.Threading.Tasks;
namespace ConsoleApp2 {
    class Program {
        static void Main(string[] args) {
        public static void UploadMultiFilesUsingMultiThread() {
            string[] filePaths = System.IO.Directory.GetFiles(@ "C: \Users\hp\Documents\1\2");
            OfficeDevPnP.Core.AuthenticationManager authenticateManager = new OfficeDevPnP.Core.AuthenticationManager();
            string webUrl = "";
            string userName = "";
            string password = "Softree2016";
            List < Action > actionsArray = new List < Action > ();
            foreach(var filePath in filePaths) {
                actionsArray.Add(new Action(() => UploadMultipleFiles(authenticateManager, webUrl, userName, password, filePath)));
            Action[] arrayList = actionsArray.ToArray();
            Parallel.Invoke(new System.Threading.Tasks.ParallelOptions {
                MaxDegreeOfParallelism = 2
            }, arrayList);
            System.Console.WriteLine("Process Completed");
        private static void UploadMultipleFiles(AuthenticationManager authMgr, string webUrl, string user, string password, string filePath) {
            ClientContext ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(webUrl, user, password);
            Web web = ctx.Web;
            List list = web.Lists.GetByTitle("Target Document Library");
            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);



We can see files uploaded in the library.

Leave a Reply

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