I have C# code for Moving Document Libraries and its Items within same Site Collection maintaining its Metadata like "Modified", "Modified by" etc.
Can anybody modify this code to move an entire Document library along with its items in another subiste?
string siteUrl = "http://sharepoint-devsite.com/DevSite";
string fileName = "ProjectPlan.docx";
string sourceDirectory = "/DevSite/Shared Documents";
string destinationDirectory = "/DevSite/ProjectDocs/ProjTeam/DevTeam/TechLead";
using (SPSite currSite = new SPSite(siteUrl))
{
using (SPWeb currWeb = currSite.OpenWeb())
{
MoveFile(currWeb, destinationDirectory, sourceDirectory, fileName);
}
}
public void MoveFile(SPWeb sourceWeb, string sourceDirectory, string destinationDirectory, string fileName)
{
SPFile sourcefile = sourceWeb.GetFile(sourceDirectory + "/" + fileName);
if (sourcefile.Exists)
{
object modifiedOn = sourcefile.Item["Modified"];
object modifiedBy = sourcefile.Item["Modified By"];
//true - replace if file exists
sourcefile.MoveTo(destinationDirectory + "/" + fileName, true);
SPFile dstFile = sourceWeb.GetFile(destinationDirectory + "/" + fileName);
SPListItem dstItem = (SPListItem)dstFile.Item;
dstItem.ParentList.Fields["Modified"].ReadOnlyField = false;
dstItem.ParentList.Fields["Modified By"].ReadOnlyField = false;
dstItem["Modified"] = modifiedOn;
dstItem["Modified By"] = modifiedBy;
//updates the item without creating another version of the item
dstItem.UpdateOverwriteVersion();
dstItem.ParentList.Fields["Modified"].ReadOnlyField = true;
dstItem.ParentList.Fields["Modified By"].ReadOnlyField = true;
}
}