1. Program file
#region global variables
static SPListItem objItemDisp;
static SPWeb objSourceWeb;
static SPWeb objDestinationWeb;
static SPFileCollection spFileCol = null;
static string strStrsourceLib ;
static string strStrdestinationLib ;
static string strSiteUrl ;
#endregion
static void Main(string[] args)
{
///Getting site url form Config File
strSiteUrl = ConfigurationManager.AppSettings["SiteURL"].ToString();
///Reading Source Library Name form Config File
strStrsourceLib = ConfigurationManager.AppSettings["SourceLib"].ToString();
///Reading Destination Library Name form Config File
strStrdestinationLib = ConfigurationManager.AppSettings["DestiLib"].ToString();
/// Start Process
StartCopyDoc();
}
/// <summary>
/// Initiate the process...
/// </summary>
private static void StartCopyDoc()
{
using (SPSite site = new SPSite(strSiteUrl))
{
try
{
using (SPWeb web = site.OpenWeb())
{
objSourceWeb = web;
objDestinationWeb = web;
objDestinationWeb.AllowUnsafeUpdates = true;
SPDocumentLibrary source_lib = objSourceWeb.Lists[strStrsourceLib] as SPDocumentLibrary;
SPFileCollection source_files = source_lib.RootFolder.Files;
RootFolderFile(source_files);
SPFolderCollection folders = web.GetFolder(strStrsourceLib).SubFolders;
SubFolders(folders);
objDestinationWeb.AllowUnsafeUpdates = false;
}
}
catch (Exception ex)
{
}
}
}
/// <summary>
/// Iteration method to fetch the doc. from nested folder
/// </summary>
/// <param name="folders"></param>
private static void SubFolders(SPFolderCollection folders)
{
foreach (SPFolder spFolder in folders)
{
if (!spFolder.Name.Equals("Forms"))
{
int len = spFolder.ToString().IndexOf('/');
string folderName = spFolder.ToString().Substring(len + 1);
SPFileCollection source_SubFolderfiles = spFolder.Files;
CreateFolder(spFolder, folderName);
RootFolderFile(source_SubFolderfiles, folderName);
SubFolders(spFolder.SubFolders);
}
}
}
/// <summary>
/// Add doc. to destination lib. in root level
/// </summary>
/// <param name="source_files"></param>
private static void RootFolderFile(SPFileCollection source_files)
{
SPDocumentLibrary destination_lib = objDestinationWeb.Lists[strStrdestinationLib] as SPDocumentLibrary;
foreach (SPFile file in source_files)
{
object modified_by = file.Item["Editor"];
object modified = file.Item["Modified"];
object created_by = file.Item["Author"];
SPFileCollection destination_files = destination_lib.RootFolder.Files;
byte[] filebytes = file.OpenBinary();
SPFile disp_file = destination_files.Add(destination_lib.RootFolder.Url + "/" + file.Name, filebytes, true);
objItemDisp = disp_file.Item as SPListItem;
objItemDisp.ParentList.Fields["Modified By"].ReadOnlyField = false;
objItemDisp.ParentList.Fields["Modified"].ReadOnlyField = false;
objItemDisp.ParentList.Fields["Created By"].ReadOnlyField = false;
objItemDisp["Editor"] = modified_by;
objItemDisp["Modified"] = modified;
objItemDisp["Author"] = created_by;
disp_file.Item.Update();
}
}
/// <summary>
/// Add doc. to Destination lib. in folder level
/// </summary>
/// <param name="source_files"></param>
/// <param name="strFolder"></param>
private static void RootFolderFile(SPFileCollection source_files,string strFolder)
{
SPDocumentLibrary destination_lib = objDestinationWeb.Lists[strStrdestinationLib] as SPDocumentLibrary;
foreach (SPFile file in source_files)
{
object modified_by = file.Item["Editor"];
object modified = file.Item["Modified"];
object created_by = file.Item["Author"];
SPFolderCollection folders = objDestinationWeb.GetFolder(strStrdestinationLib).SubFolders;
spFileCol = null;
SPFileCollection destination_files = DestinFileCollection(folders,strFolder);
byte[] filebytes = file.OpenBinary();
SPFile disp_file = destination_files.Add(destination_lib.RootFolder.Url + "/" +strFolder+"/"+ file.Name, filebytes, true);
objItemDisp = disp_file.Item as SPListItem;
objItemDisp.ParentList.Fields["Modified By"].ReadOnlyField = false;
objItemDisp.ParentList.Fields["Modified"].ReadOnlyField = false;
objItemDisp.ParentList.Fields["Created By"].ReadOnlyField = false;
objItemDisp["Editor"] = modified_by;
objItemDisp["Modified"] = modified;
objItemDisp["Author"] = created_by;
disp_file.Item.Update();
}
}
/// <summary>
/// get distination nested folder filecollection
/// </summary>
/// <param name="folders"></param>
/// <param name="fdUrl"></param>
/// <returns></returns>
private static SPFileCollection DestinFileCollection(SPFolderCollection folders, string fdUrl)
{
foreach (SPFolder spFolder in folders)
{
if (!spFolder.Name.Equals("Forms"))
{
int len = spFolder.ToString().IndexOf('/');
string folderName = spFolder.ToString().Substring(len + 1);
if (folderName.Equals(fdUrl))
{
spFileCol = spFolder.Files;
break;
}
DestinFileCollection(spFolder.SubFolders, fdUrl);
}
}
return spFileCol;
}
/// <summary>
/// Create folders at destination lib.
/// </summary>
/// <param name="spFolder"></param>
/// <param name="fldURL"></param>
private static void CreateFolder(SPFolder spFolder,string fldURL)
{
using (SPSite site=new SPSite(strSiteUrl))
{
using (SPWeb web=site.OpenWeb())
{
object modified_by = spFolder.Item["Editor"];
object modified = spFolder.Item["Modified"];
object created = spFolder.Item["Created"];
object created_by = spFolder.Item["Author"];
SPDocumentLibrary destination_lib = objDestinationWeb.Lists[strStrdestinationLib] as SPDocumentLibrary;
SPFolderCollection spFolderCol = web.Folders;
spFolderCol.Add(strSiteUrl + "/" + strStrdestinationLib + "/" + fldURL + "/");
SPFolder spf=web.GetFolder(strSiteUrl + "/" + strStrdestinationLib + "/" + fldURL );
SPListItem objDisp = spf.Item as SPListItem;
objDisp.ParentList.Fields["Modified By"].ReadOnlyField = false;
objDisp.ParentList.Fields["Modified"].ReadOnlyField = false;
objDisp.ParentList.Fields["Created By"].ReadOnlyField = false;
objDisp["Editor"] = modified_by;
objDisp["Modified"] = modified;
objDisp["Author"] = created_by;
spf.Item.Update();
destination_lib.Update();
}
}
}
2. configuration file
<appSettings>
<add key="SiteURL" value="http://sp2013vm:1616/sites/HSE"/>
<add key="SourceLib" value="Test1"/>
<add key="DestiLib" value="Test2"/>
</appSettings>