What if you need to move all the files in one document library to a library in a different web app. Along with this, you also need to maintain your metadata. This includes the content types, file created by, created on, modified, and modified by metadata.
In my Dev environment, I created a simple Windows Form app.
Add a reference to the SharePoint Dll.
I named my app MoveDataSP.
In the scenario below, the code will copy the contents of folder1 from the sourceSPsite to the destinationSPsite. Both sites have libaries named the same (libraryName).
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.SharePoint; namespace MoveDataSP { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static void CopyFolderToAnotherSiteCollection(string sourceUrl, string destinationUrl, string listName, string folderUrl, string folderName) { string listRelatvieFolderUrl = folderUrl.Substring(folderUrl.IndexOf("/") + 1); using (SPSite sourceSite = new SPSite(sourceUrl)) { using (SPWeb sourceWeb = sourceSite.OpenWeb()) { SPFolder sourceFolder = sourceWeb.GetFolder(folderUrl); using (SPSite destSite = new SPSite(destinationUrl)) { using (SPWeb destWeb = destSite.OpenWeb()) { if (!destWeb.GetFolder(folderUrl).Exists) { SPListItem destItem = destWeb.Lists[listName].Items.Add(string.Empty, SPFileSystemObjectType.Folder, listRelatvieFolderUrl); destItem.Update(); } SPFolder destFolder = destWeb.GetFolder(folderUrl); foreach (SPFile file in sourceFolder.Files) { SPUser sCreatedby = destWeb.AllUsers[file.Author.ToString()]; SPUser sModifiedBy = destWeb.AllUsers[file.ModifiedBy.ToString()]; DateTime dCreated = file.TimeCreated; DateTime dModified = file.TimeLastModified; SPFile sFile = destFolder.Files.Add(file.Url, file.OpenBinary(), file.Properties, sCreatedby, sModifiedBy, dCreated, dModified, true); SPListItem item = sFile.Item; item["Created By"] = sCreatedby; item["Modified By"] = sModifiedBy; item["Created"] = dCreated; item["Modified"] = dModified; sFile.Item.SystemUpdate(); } foreach (SPFolder folder in sourceFolder.SubFolders) { CopyFolderToAnotherSiteCollection(sourceUrl, destinationUrl, listName, folder.Url, folder.Name); } } } } } } private void Form1_Load(object sender, EventArgs e) { CopyFolderToAnotherSiteCollection("http://sourceSPsite/", "http://destinationSPsite", "libraryName", "libraryName/folder1", "folder1"); } } }
Most of the code above was borrowed from this post:
http://sharepointfieldnotes.blogspot.com/2009/11/how-to-copy-files-across-site.html