Reputation: 131
How would I go about uploading an entire directory and all sub-directories and files to a SharePoint 2010 Server?
I don't think this function is specifically built into SharePoint (just uploading multiple documents in one folder). But the way I understand it, I can write something in VB or C# to accomplish this. How would I go about doing this?
Or is there an easier way to do it? The directory I want to upload is huge, so going through folders one by one is out of the question.
Upvotes: 0
Views: 6496
Reputation: 838
i will also suggest using SharePoint object model here is the below code:
static void Main(string[] args)
{
SPSite site = new SPSite("site url");
SPWeb web = site.OpenWeb();
string stitle = web.Title;
string localPath = "local path";
string documentLibraryName = "Documents";
CreateDirectories(localPath,web.Folders[documentLibraryName].SubFolders);
}
static 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);
spfile.Update();
}
//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);
spNewFolder.Update();
//Recursive call to create child folder
CreateDirectories(oDI.FullName, spNewFolder.SubFolders);
}
}
Upvotes: 0
Reputation: 10345
In SharePoint 2010, this functionality is provided out of the box.
From the Microsoft SharePoint Team Blog:
To add an item to this library, you can click on the Add document button in the view. That button will always be available at the end of the current page, if you want to quickly add documents to this library. When you click it, you’ll notice that instead of navigating the entire page, we just put up a dialog asking you where you want to upload. This makes it faster to load and also easier to understand what’s going on. For this post, I actually want to upload multiple files – so go ahead and click on Upload Multiple Files.
...
You can drag files onto the blue rectangle to add them to your upload list, or you can click on Browse for files instead to find the files in a windows dialog. Once you’ve picked them, click Ok and they will start uploading
I just tried this in a SharePoint 2010 Document Library. I created a hierarchy of folders and added a couple notepad files to it. I then dragged the topmost folder into the Upload Multiple Files dialog and it uploaded that folder along with all of its subfolders and files.
Note that use of Upload Multiple Files requires Silverlight on the client.
Upvotes: 0
Reputation: 7892
In case you need the codes here it is for mass checkin and mass check outs, as well as recursive copy, do not use .Net 4 Framework as you will get this error
Unhandled Exception: System.PlatformNotSupportedException: Microsoft SharePoint
is not supported with version 4.0.30319.1 of the Microsoft .Net Runtime.
at Microsoft.SharePoint.Administration.SPConfigurationDatabase.get_Farm()
at Microsoft.SharePoint.Administration.SPFarm.FindLocal(SPFarm& farm, Boolean
& isJoined)
at Microsoft.SharePoint.SPSite..ctor(String requestUrl)
at SharepointCopy.MassCheckOut()
at SharepointCopy.Process()
at Program.Main(String[] args)
So I suggest to use .Net 3.5
using System;
using Microsoft.SharePoint;
using System.IO;
public static void RecursiveMassCheckIn()
{
using (SPSite oSharepointSite = new SPSite("http://sharepoint.com/MyTeamSite"))
{
using (SPWeb oSharepointWeb = oSharepointSite.OpenWeb())
{
SPDocumentLibrary oSharepointDocs = (SPDocumentLibrary)oSharepointWeb.Lists["MyDocumentLibrary"];
int iFolderCount = oSharepointDocs.Folders.Count;
//Check in whats on root
MassCheckIn(oSharepointDocs.RootFolder);
//Check in whats on subfolders
for (int i = 0; i < iFolderCount; i++)
{
MassCheckIn(oSharepointDocs.Folders[i].Folder);
}
}
}
}
public static void MassCheckIn(SPFolder oSharepointFolder)
{
foreach (SPFile oSharepointFiles in oSharepointFolder.Files)
{
if (oSharepointFiles.CheckOutType != SPFile.SPCheckOutType.None)
{
oSharepointFiles.CheckIn("Programmatically Checked In");
}
}
}
public static void RecursiveCopy(string sSourceFolder, string sDestinationFolder)
{
if (!Directory.Exists(sDestinationFolder))
{
Directory.CreateDirectory(sDestinationFolder);
}
string[] aFiles = Directory.GetFiles(sSourceFolder);
foreach (string sFile in aFiles)
{
string sFileName = Path.GetFileName(sFile);
string sDestination = Path.Combine(sDestinationFolder, sFileName);
File.Copy(sFile, sDestination);
}
string[] aFolders = Directory.GetDirectories(sSourceFolder);
foreach (string sFolder in aFolders)
{
string sFileNameSub = Path.GetFileName(sFolder);
string sDestinationSub = Path.Combine(sDestinationFolder, sFileNameSub);
RecursiveCopy(sFolder, sDestinationSub);
}
}
then run
RecursiveCopy(@"C:\LocalFolder\", @"\\sharepoint.com\MyTeamSite\MyDocumentLibrary\");
RecursiveMassCheckIn();
Upvotes: 2
Reputation: 28128
Change the view to "Explorer View" and you can drag-and-drop files from a Windows client machine. To do it programmatically, you can just copy files to the UNC path like \\SERVERNAME\path\to\documentlibrary
Note that in WSS 3.0/MOSS 2007 there is an issue if you have versioning enabled on the document library, then you have to "check in" each document after you've dragged 'em in in Explorer View. (One work-around in that case is you can disable versioning before adding the files.) I don't know if this is still an issue in SP 2010.
Upvotes: 4