{"id":7002,"date":"2021-09-27T11:25:55","date_gmt":"2021-09-27T11:25:55","guid":{"rendered":"https:\/\/softreetechnology.com\/?p=7002"},"modified":"2021-09-27T11:25:57","modified_gmt":"2021-09-27T11:25:57","slug":"implementing-multi-threading-with-maxdegreeofparallelism-for-uploading","status":"publish","type":"post","link":"https:\/\/softreetechnology.com\/blog\/sharepoint\/implementing-multi-threading-with-maxdegreeofparallelism-for-uploading\/","title":{"rendered":"Implementing Multi-Threading With MaxDegreeOfParallelism For Uploading"},"content":{"rendered":"\n<p>In this&nbsp;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 <strong>MaxDegreeOfParallelism<\/strong> Parameter. MaxDegreeOfParallelism Parameter is the one, which decides how many items in the array are to be taken at a time simultaneously.<\/p>\n\n\n\n<p>These are the below code we need to implement,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\nusing Microsoft.SharePoint.Client;\nusing System.IO;\nusing OfficeDevPnP.Core;\nusing System.Threading.Tasks;\nnamespace ConsoleApp2 {\n    class Program {\n        static void Main(string&#91;] args) {\n            UploadMultiFilesUsingMultiThread();\n        }\n        public static void UploadMultiFilesUsingMultiThread() {\n            string&#91;] filePaths = System.IO.Directory.GetFiles(@ \"C: \\Users\\hp\\Documents\\1\\2\");\n            OfficeDevPnP.Core.AuthenticationManager authenticateManager = new OfficeDevPnP.Core.AuthenticationManager();\n            string webUrl = \"<a href=\"https:\/\/softreepriya.sharepoint.com\/sites\/demosite\/\">https:\/\/softreepriya.sharepoint.com\/sites\/demosite\/<\/a>\";\n            string userName = \"<a href=\"mailto:demo@softreepriya.onmicrosoft.com\">demo@softreepriya.onmicrosoft.com<\/a>\";\n            string password = \"Softree2016\";\n            List &lt; Action &gt; actionsArray = new List &lt; Action &gt; ();\n            foreach(var filePath in filePaths) {\n                actionsArray.Add(new Action(() =&gt; UploadMultipleFiles(authenticateManager, webUrl, userName, password, filePath)));\n            }\n            Action&#91;] arrayList = actionsArray.ToArray();\n            Parallel.Invoke(new System.Threading.Tasks.ParallelOptions {\n                MaxDegreeOfParallelism = 2\n            }, arrayList);\n            System.Console.WriteLine(\"Process Completed\");\n            System.Console.ReadLine();\n        }\n        private static void UploadMultipleFiles(AuthenticationManager authMgr, string webUrl, string user, string password, string filePath) {\n            ClientContext ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(webUrl, user, password);\n            Web web = ctx.Web;\n            ctx.Load(web);\n            List list = web.Lists.GetByTitle(\"Target Document Library\");\n            ctx.Load(list);\n            ctx.Load(list.RootFolder);\n            ctx.ExecuteQueryRetry();\n            if (System.IO.File.Exists(filePath)) {\n                System.Console.ForegroundColor = ConsoleColor.Green;\n                System.Console.WriteLine(\"Entered into : \" + Path.GetFileName(filePath));\n                Folder folder = web.GetFolderByServerRelativeUrl(list.RootFolder.ServerRelativeUrl);\n                folder.UploadFile(Path.GetFileName(filePath), filePath, true);\n                folder.Update();\n                ctx.Load(folder);\n                ctx.ExecuteQueryRetry();\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>C#<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<p>We can see files uploaded in the library.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/csharpcorner-mindcrackerinc.netdna-ssl.com\/UploadFile\/BlogImages\/09242021111837AM\/priiiii.png\" alt=\"\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this&nbsp;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-7002","post","type-post","status-publish","format-standard","hentry","category-sharepoint"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Implementing Multi-Threading With MaxDegreeOfParallelism For Uploading - Softree Technology<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/implementing-multi-threading-with-maxdegreeofparallelism-for-uploading\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Implementing Multi-Threading With MaxDegreeOfParallelism For Uploading - Softree Technology\" \/>\n<meta property=\"og:description\" content=\"In this&nbsp;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 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/implementing-multi-threading-with-maxdegreeofparallelism-for-uploading\/\" \/>\n<meta property=\"og:site_name\" content=\"Softree Technology\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-27T11:25:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-27T11:25:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/csharpcorner-mindcrackerinc.netdna-ssl.com\/UploadFile\/BlogImages\/09242021111837AM\/priiiii.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Implementing Multi-Threading With MaxDegreeOfParallelism For Uploading - Softree Technology","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/softreetechnology.com\/blog\/sharepoint\/implementing-multi-threading-with-maxdegreeofparallelism-for-uploading\/","og_locale":"en_US","og_type":"article","og_title":"Implementing Multi-Threading With MaxDegreeOfParallelism For Uploading - Softree Technology","og_description":"In this&nbsp;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 [&hellip;]","og_url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/implementing-multi-threading-with-maxdegreeofparallelism-for-uploading\/","og_site_name":"Softree Technology","article_published_time":"2021-09-27T11:25:55+00:00","article_modified_time":"2021-09-27T11:25:57+00:00","og_image":[{"url":"https:\/\/csharpcorner-mindcrackerinc.netdna-ssl.com\/UploadFile\/BlogImages\/09242021111837AM\/priiiii.png","type":"","width":"","height":""}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/implementing-multi-threading-with-maxdegreeofparallelism-for-uploading\/#article","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/implementing-multi-threading-with-maxdegreeofparallelism-for-uploading\/"},"author":{"name":"admin","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b"},"headline":"Implementing Multi-Threading With MaxDegreeOfParallelism For Uploading","datePublished":"2021-09-27T11:25:55+00:00","dateModified":"2021-09-27T11:25:57+00:00","mainEntityOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/implementing-multi-threading-with-maxdegreeofparallelism-for-uploading\/"},"wordCount":106,"commentCount":0,"publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/implementing-multi-threading-with-maxdegreeofparallelism-for-uploading\/#primaryimage"},"thumbnailUrl":"https:\/\/csharpcorner-mindcrackerinc.netdna-ssl.com\/UploadFile\/BlogImages\/09242021111837AM\/priiiii.png","articleSection":["SharePoint"],"inLanguage":"en","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/implementing-multi-threading-with-maxdegreeofparallelism-for-uploading\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/implementing-multi-threading-with-maxdegreeofparallelism-for-uploading\/","url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/implementing-multi-threading-with-maxdegreeofparallelism-for-uploading\/","name":"Implementing Multi-Threading With MaxDegreeOfParallelism For Uploading - Softree Technology","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/implementing-multi-threading-with-maxdegreeofparallelism-for-uploading\/#primaryimage"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/implementing-multi-threading-with-maxdegreeofparallelism-for-uploading\/#primaryimage"},"thumbnailUrl":"https:\/\/csharpcorner-mindcrackerinc.netdna-ssl.com\/UploadFile\/BlogImages\/09242021111837AM\/priiiii.png","datePublished":"2021-09-27T11:25:55+00:00","dateModified":"2021-09-27T11:25:57+00:00","breadcrumb":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/implementing-multi-threading-with-maxdegreeofparallelism-for-uploading\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/implementing-multi-threading-with-maxdegreeofparallelism-for-uploading\/"]}]},{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/implementing-multi-threading-with-maxdegreeofparallelism-for-uploading\/#primaryimage","url":"https:\/\/csharpcorner-mindcrackerinc.netdna-ssl.com\/UploadFile\/BlogImages\/09242021111837AM\/priiiii.png","contentUrl":"https:\/\/csharpcorner-mindcrackerinc.netdna-ssl.com\/UploadFile\/BlogImages\/09242021111837AM\/priiiii.png"},{"@type":"BreadcrumbList","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/implementing-multi-threading-with-maxdegreeofparallelism-for-uploading\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/softreetechnology.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Implementing Multi-Threading With MaxDegreeOfParallelism For Uploading"}]},{"@type":"WebSite","@id":"https:\/\/softreetechnology.com\/blog\/#website","url":"https:\/\/softreetechnology.com\/blog\/","name":"Softree Technology","description":"Celebrating 10+ Years in SharePoint Consulting !","publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/softreetechnology.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en"},{"@type":"Organization","@id":"https:\/\/softreetechnology.com\/blog\/#organization","name":"Softree Technology","url":"https:\/\/softreetechnology.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","contentUrl":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","width":844,"height":230,"caption":"Softree Technology"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b","name":"admin","image":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/softreeconsulting.com"],"url":"https:\/\/softreetechnology.com\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/7002","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=7002"}],"version-history":[{"count":1,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/7002\/revisions"}],"predecessor-version":[{"id":7003,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/7002\/revisions\/7003"}],"wp:attachment":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=7002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=7002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=7002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}