Click here to Skip to main content
15,174,684 members
Articles / Web Development / ASP.NET
Posted 10 Aug 2010


39 bookmarked

Uploading Files and Folders Recursively to a SharePoint Site

Rate me:
Please Sign up or sign in to vote.
4.64/5 (10 votes)
10 Aug 2010CC (ASA 2.5)1 min read
This article demonstrates how to upload files and folders recursively from a local folder to Microsoft SharePoint Foundation site using the Object Model.


You can upload documents to SharePoint libraries using the Object Model or SharePoint Webservices and this article about how to upload a file and folders recursively from a local folder to a folder on a Microsoft SharePoint Foundation site using the Object Model.



I was assigned a project to Migrate DOORS (Dynamic Object-Oriented Requirements System) data into the TFS 2010 SharePoint portal server. The DXL script is take care of the extraction part and stored the files into the local folders. Then I Googled around to find a way to upload files recursively to SharePoint server, but no use. So I decided to do some research on it and came up with this code.


The Microsoft.SharePoint.dll is used to access the Sharepoint object model.

Code for Uploading File to Document Library

	localPath = txtLocalPath.Text; 		//Local path.
	sharePointSite = txtServerURL.Text; 		//SharePoint site URL.
	documentLibraryName = txtLibrary.Text; 	// SharePoint library name.

	// Make an object of your SharePoint site.
	using (SPSite oSite = new SPSite(sharePointSite))
		oWeb = oSite.OpenWeb();
catch (Exception ex)
	MessageBox.Show("Error:" + ex.Message );

CreateDirectories(string path, SPFolderCollection oFolderCollection)

The CreateDirectories method is a recursive method which accepts two parameters. The path parameter specifies the path of the source location in the file system of the local computer, and the oFolderCollection parameter specifies the folder collection of the destination.

private void CreateDirectories(string path, SPFolderCollection oFolderCollection)
	//Upload Multiple Files
	foreach (FileInfo oFI in new DirectoryInfo(path).GetFiles())
		FileStream fileStream = File.OpenRead(oFI.FullName);
		SPFile spfile = oFolderCollection.Folder.Files.Add
					(oFI.Name, fileStream, true);
	//Upload Multiple Folders
	foreach (DirectoryInfo oDI in new DirectoryInfo(path).GetDirectories())
		string sFolderName = oDI.FullName.Split('\\')
					[oDI.FullName.Split('\\').Length - 1];
		SPFolder spNewFolder = oFolderCollection.Add(sFolderName);
		//Recursive call to create child folder
		CreateDirectories(oDI.FullName, spNewFolder.SubFolders);

Local Folder


SharePoint Library





  • The size of the file that is uploaded cannot exceed 2 GB.
  • For all this code to execute, current logged in user should have create and upload, delete permissions at respective document library.


  • 10th August, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


About the Author

BalaG Ganesan
Technical Lead Infosys
India India
Working as a Technology Lead in Infosys at Chennai, India.

Comments and Discussions

QuestionApp Crashes with error in PresentationFramework.dll Win 8,1 Studio Community 2013 Pin
Leach72387-May-15 11:57
MemberLeach72387-May-15 11:57 
QuestionSolution Created as a webpart Pin
Jhonny Marcelo24-Jun-14 12:59
MemberJhonny Marcelo24-Jun-14 12:59 
QuestionI have a custom list Manage Requisition Groups and Two folder content type Requisition Parent Group and Requisition Sub-Group.Each Folder conent type contains site columns Name, Parent Group and Group users.I want to populate list item in a treeview. Pin
Deempy16-Jul-12 6:05
MemberDeempy16-Jul-12 6:05 
QuestionError: Value does not fall within the expected range. Pin
RittikaKumar7-May-12 10:05
MemberRittikaKumar7-May-12 10:05 
QuestionDXL script to save Word document Pin
szczy75-Jan-11 6:48
Memberszczy75-Jan-11 6:48 
GeneralThere's an easier way Pin
NetDave11-Aug-10 9:20
MemberNetDave11-Aug-10 9:20 
GeneralRe: There's an easier way Pin
kaufmed8-Sep-10 11:52
Memberkaufmed8-Sep-10 11:52 
GeneralRe: There's an easier way Pin
Sevententh13-Dec-11 23:25
MemberSevententh13-Dec-11 23:25 
GeneralYou should probably remove your TFS bindings before uploading the code Pin
kaschimer10-Aug-10 6:10
Memberkaschimer10-Aug-10 6:10 
GeneralRe: You should probably remove your TFS bindings before uploading the code Pin
BalaG Ganesan10-Aug-10 8:28
MemberBalaG Ganesan10-Aug-10 8:28 
GeneralRe: You should probably remove your TFS bindings before uploading the code Pin
L Hills17-Aug-10 3:58
MemberL Hills17-Aug-10 3:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.