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.ParentList.Fields["Modified"].ReadOnlyField = true;
        dstItem.ParentList.Fields["Modified By"].ReadOnlyField = true;


2 Answers 2

    #region global variables
    SPListItem item_disp;
    SPWeb source_web;
    SPWeb destination_web;
    bool flag1 = false;
    bool flag2 = false;

    protected void Page_Load(object sender, EventArgs e)
        using (SPSite site = new SPSite(SPContext.Current.Site.Url))

                foreach(SPWeb web in site.AllWebs)
                    if (web.Name == "migration_test")
                        source_web = web;
                        flag1 = true;

                    if (web.Name == "migration_test2")
                        destination_web = web;
                        flag2 = true;

                    if (flag1 && flag2)


                destination_web.AllowUnsafeUpdates = true;
                SPDocumentLibrary source_lib = source_web.Lists["source_lib"] as SPDocumentLibrary;

                SPFileCollection source_files = source_lib.RootFolder.Files;//SubFolders[DropDownList2.SelectedItem.Text].Files;

                SPDocumentLibrary destination_lib = destination_web.Lists["destination_lib"] 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);

                        item_disp = disp_file.Item as SPListItem;
                        item_disp.ParentList.Fields["Modified By"].ReadOnlyField = false;
                        item_disp.ParentList.Fields["Modified"].ReadOnlyField = false;
                        item_disp.ParentList.Fields["Created By"].ReadOnlyField = false;

                        item_disp["Editor"] = modified_by;
                        item_disp["Modified"] = modified;
                        item_disp["Author"] = created_by;


                destination_web.AllowUnsafeUpdates = false;
            catch (Exception ex)



this code moves all documents from document library in one site to a document library in another site currently only Created By,Modified By, and Modified fields are preserved. It is a simple visual webpart. But one thing is that in this code folder structure is not mantained


Yes it is possible to move documents from one document library to another library using server object model. Use the method MoveTo of SPFile to move documents from source to destination. Refer this http://msdn.microsoft.com/en-us/library/ms468280.aspx.

Preserving the metadata

Make sure that source and destination libraries have same set of fields/content types. Before moving the data from source, read all field values include "Author", "Modified" etc and after the document is moved to destination, get the SPListItem object associated with the SPFile object and update all field values and finally call SPListItem.SystemUpdate(false)

  • please see my updated question. it moves one file to another document library but I want entire folder itself to move. Mar 3, 2014 at 5:49
  • You will have to write the code for keeping the same folder structure in destination library. I don't have the code available. The other non programmatic way to do this is to save the library as template including contents and create a new library using that template. Mar 3, 2014 at 5:54
  • but it will not maintain metadata. Mar 3, 2014 at 6:00
  • Yes it wont retain the field values like Modified, author etc. You will have to update them using code as i mentioned in my answer. Mar 3, 2014 at 6:02
  • I have added code for copying just one file.can you able to modify it for copying all folders including subfolder if possible? Mar 3, 2014 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.