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.
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
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
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;
MassCheckOut(oSharepointDocs.RootFolder);
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
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;
MassCheckIn(oSharepointDocs.RootFolder);
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:
RecursiveCopy(@"C:\LocalFolder\", @"\\sharepoint.com\MyTeamSite\MyDocumentLibrary\");
RecursiveMassCheckIn();