Programatically Copy and Check In a Full Directory to Sharepoint

By | October 7, 2010


I remember months ago after I deployed Sharepoint 2010 in where I work, questions start to pour on how they will use it, and people in our company do understand the principles of Sharepoint so its a bit easy to explain, but there was one question that stood out as they were using file servers before to store documents and they want to start migrating those to Sharepoint to leverage the search functions.  So they started to copy files to our Sharepoint instance then all of a sudden they realized it was a big task to do that specially if you have a document repository that they had grown more than 5 years ago.  If you do it within the UI of Sharepoint it will be really slow so they asked whether they can do it quickly in one shot like copy and pasting to a file system and my answer was definitely yes as there is a function on Sharepoint under the library tools to open a certain library using Windows explorer, you can see it illustrated below.

Clicking that will open the Windows Explorer and you can drag and drop files like a normal file system, then another question came into their mind as the one they had recently uploaded cannot be searched, I told them search for it after 30 minutes as it wont show on search in real time as it has to be indexed first by sharepoint.

30 minutes after…

They still can’t find it.  I had a look and everything seems to be on the proper location but wait, the icons of the files have a green arrow down below which means the document library had its versioning turned on and the files so the files by default are checked out and since it havent been checked in, it wont show on the search results unless it was checked in for at least once in the files lifetime.  So I told them to check it in manually by selecting the files they need to check-in and commiting by clicking the check in button.  They were satisfied.

Note: if the versioning is not turned on in the document library there will be no issue in checking in or out

Now recently as I was browsing Stack Overflow I saw an answer relating to programatically copying files to sharepoint (http://stackoverflow.com/questions/3876598/sharepoint-2010-uploading-documents/3876843#3876843) and I suddently thought why havent I think of that.  Do the checkin programatically, so thats what I tried and here is how I have done it.

Use the following references, you must also download Sharepoint 2010 SDK.


using System;
using Microsoft.SharePoint;
using System.IO;

First you need a function to recursively copy files and directories from one location to another, and since the files on sharepoint can be addressed using the file system its easy to use the System.IO classes.  To know which is the directory you want to copy to just check what is the address on the Explorer view discussed above but usually if its http://test.com/subfolder it will be \test.comsubfolder just remove the http: and use backslash instead.

Recursive Directory Copy

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);
    }
}

Now for the codes you need to have a function to checkout files recursively.  Just replace the the SPSite to your Team Site http address and SPDocumentLibrary to the name of your document library.

Programatically Checking out Files Recusively in Sharepoint

public static void RecursiveMassCheckOut()
{
    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;

            //Checkouot whats on root
            MassCheckOut(oSharepointDocs.RootFolder);    

            //Checkout whats on subfolders
            for (int i=0; i < iFolderCount; i++)
            {
                MassCheckOut(oSharepointDocs.Folders[i].Folder);
            }

        }
    }
}
public static void MassCheckOut(SPFolder oSharepointFolder)
{
    foreach (SPFile oSharepointFiles in oSharepointFolder.Files)
    {
        if (oSharepointFiles.CheckOutType == SPFile.SPCheckOutType.None)
        {
            oSharepointFiles.CheckOut();
        }
    }
}

Now as an added bonus here are the codes for checking in files recursively

Programatically Checking in Files Recusively in Sharepoint

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");
        }
    }

}

Now I am using .Net 4 Framework and when I started to use my first build I had this error.

So it looks like its is not yet suported on .Net 4 so better build it in .Net 3.5 Framework after that it will all work fine.

Now you can call your functions like such.

RecursiveCopy(@"C:LocalFolder"@"\sharepoint.comMyTeamSiteMyDocumentLibrary");
RecursiveMassCheckIn();
Recommended

3 thoughts on “Programatically Copy and Check In a Full Directory to Sharepoint

  1. olver

    Hi, i gotta the next question, the RecursiveCopy(sFolder, sDestinationSub); required 3 parameteres when u programming?

    thanks!

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.