Click here to Skip to main content
15,867,937 members
Articles / Desktop Programming / WPF

Programatically Copy and Check In a Full Directory to Sharepoint

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
6 Oct 2010CPOL3 min read 24.5K   9  
Programatically Copy and Check In a Full Directory to Sharepoint

I remember months ago after I deployed Sharepoint 2010 in where I work, questions started to pour in on how they will use it. People in our company do understand the principles of Sharepoint so it's 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 wanted 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 especially if you have a document repository that 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 in 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 minds as the one they had recently uploaded could not be searched. I told them to search for it after 30 minutes as it won't show on search in real time as it has to be indexed first by Sharepoint.

30 minutes later…

They still can't find it. I had a look and everything seemed to be in 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, so the files by default are checked out and since it hasn't been checked in, it won't show on the search results unless it was checked in at least once in the file's lifetime. So I told them to check it in manually by selecting the files they need to check-in and committing 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 programmatically copying files to Sharepoint and I suddenly thought why haven't I thought of that. Do the check in programmatically, so that's what I tried and here is how I have done it.

Use the following references. You must also download Sharepoint 2010 SDK.

C#
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, it's 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 it's http://test.com/ subfolder, it will be \\test.com\subfolder, just remove the http: and use backslash instead.

Recursive Directory Copy

C#
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 check out files recursively. Just replace the SPSite to your Team Site http address and SPDocumentLibrary to the name of your document library.

Programmatically Checking Out Files Recursively in Sharepoint

C#
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;

            //Check out what's in root
            MassCheckOut(oSharepointDocs.RootFolder);    

            //Check out what's in 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.

Programmatically Checking in Files Recursively in Sharepoint

C#
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 what's in root
            MassCheckIn(oSharepointDocs.RootFolder);

            //Check in what's in 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 it's not yet supported 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:

C#
RecursiveCopy(@"C:\LocalFolder\", @"\\sharepoint.com\MyTeamSite\MyDocumentLibrary\");
RecursiveMassCheckIn();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
New Zealand New Zealand
http://nz.linkedin.com/in/macaalay
http://macaalay.com/

Comments and Discussions

 
-- There are no messages in this forum --