views:

150

answers:

4

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.

A: 

Try using a PUT WebRequest, like used here.

Kit Menke
+3  A: 

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.

ewall
I agree with this answer and I am using SP2010 the issue remains the same users still need to check in the files. Also one thing to note "Explorer View" only works on IE.
Raymund
A: 

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();
Raymund
A: 

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.

Rich Bennema