locked
Sharepoint Export Import List Items RRS feed

  • Question

  • Hello,

    My requirement is to export the list items in Pages library with its meta data i.e. Created Time and Modified Time.

    I am trying Export Import code, but it does not import time. It imports the CreatedBy and ModifiedBY acuurately but createdTIme and Modified Time comes as current time, the time i run the import.

    Pls help

    Code:

    private static void ExportItems()
        {
          string exportFolder = @"C:\exports\";
          string exportFileName = "Export_File.cmp";
    
          using (SPSite site = new SPSite(sourceSiteUrl))
          {
            using (SPWeb oWeb = site.OpenWeb())
            {
              SPList oList = oWeb.Lists["Pages"];
              SPListItem oItemA = oList.GetItemById(2480);
    
              SPExportObject exportObject = new SPExportObject();
              exportObject.Id = oItemA.UniqueId;
              exportObject.Type = SPDeploymentObjectType.ListItem;
    
              SPExportSettings exportSettings = new SPExportSettings();
              exportSettings.LogExportObjectsTable = true;
              exportSettings.LogFilePath = exportFolder + "Exportlog.log";
              exportSettings.SiteUrl = sourceSiteUrl;
              exportSettings.ExportMethod = SPExportMethodType.ExportAll;
              exportSettings.FileLocation = exportFolder;
              exportSettings.BaseFileName = exportFileName;
              exportSettings.ExportObjects.Add(exportObject);
              exportSettings.OverwriteExistingDataFile = false;
    
              exportSettings.ExcludeDependencies = true;
    
              SPExport export = new SPExport(exportSettings);
              export.Run();
    
              SPImportSettings settings = new SPImportSettings();
              settings.SiteUrl = destinationSiteUrl;
              settings.WebUrl = destinationSiteUrl;
              settings.FileLocation = exportFolder;
              settings.LogFilePath = exportFolder + "Importlog.log";
              settings.RetainObjectIdentity = false;
              settings.BaseFileName = exportFileName;
              settings.UserInfoDateTime = SPImportUserInfoDateTimeOption.ImportAll;
    
              SPImport import = new SPImport(settings);
              // handle the import event to re-parent the item           
              import.Started += new EventHandler<SPDeploymentEventArgs>(import_Started);
              import.Run();
            }
          }
        }
    
        static void import_Started(object sender, SPDeploymentEventArgs e) 
         { 
           
           using (SPSite site = new SPSite(destinationSiteUrl)) 
           { 
             using (SPWeb oWeb = site.OpenWeb()) 
             { 
              SPList oList = oWeb.Lists["Pages"]; 
      
               foreach (SPImportObject io in e.RootObjects)  
              {  
                if (io.Type == SPDeploymentObjectType.ListItem)         
                { 
                  io.TargetParentUrl = oList.RootFolder.ServerRelativeUrl;  
                   
                }                                                                       
              }  
             } 
          } 
         } 

     

    Tuesday, October 5, 2010 12:18 PM

Answers

  • Hi Karnek,

     

                    My sample code is in much the same way as you, but it works fine. Here is it.

     

                    protected static SPSite siteCollection = new SPSite("http://porter:1130");

            //protected static SPSite siteCollection = new SPSite("http://porter");

            protected static SPWeb site = siteCollection.OpenWeb();

            static void Main(string[] args)

            {

                SPSecurity.RunWithElevatedPrivileges(delegate()

                {

                    using (siteCollection)

                    {

                        using (site)

                        {

    string exportFolder = @"C:\exports\";

                string exportFileName = "Export_File.cmp";

     

                SPList oList = site.Lists["www"];

                foreach (SPListItem item in oList.Items ) {

                    Console.WriteLine(item.ID);

                }

                SPListItem oItemA = oList.GetItemById(5);

     

                SPExportObject exportObject = new SPExportObject();

                exportObject.Id = oItemA.UniqueId;

                exportObject.Type = SPDeploymentObjectType.ListItem;

     

                SPExportSettings exportSettings = new SPExportSettings();

                exportSettings.LogExportObjectsTable = true;

                exportSettings.LogFilePath = exportFolder + "Exportlog.log";

                exportSettings.SiteUrl = "http://porter:1130";

                exportSettings.ExportMethod = SPExportMethodType.ExportAll;

                exportSettings.FileLocation = exportFolder;

                exportSettings.BaseFileName = exportFileName;

                exportSettings.ExportObjects.Add(exportObject);

                exportSettings.OverwriteExistingDataFile = true;

     

                exportSettings.ExcludeDependencies = true;

     

                SPExport export = new SPExport(exportSettings);

                export.Run();

     

                SPImportSettings settings = new SPImportSettings();

                settings.SiteUrl = "http://porter";

                settings.WebUrl = "http://porter";

                settings.FileLocation = exportFolder;

                settings.LogFilePath = exportFolder + "Importlog.log";

                settings.RetainObjectIdentity = false;

                settings.BaseFileName = exportFileName;

                settings.UserInfoDateTime = SPImportUserInfoDateTimeOption.ImportAll;

     

                SPImport import = new SPImport(settings);

                import.Run();

                        }

                    }

                });

     

            }

     

    Best Regards,

    Porter Wang

    • Proposed as answer by Arup Biswas Tuesday, October 12, 2010 6:22 AM
    • Marked as answer by KeFang Chen Friday, October 15, 2010 5:59 AM
    Tuesday, October 12, 2010 3:14 AM

All replies

  • Hi Karnek,

     

                   Thanks to share your problem.

     

                    For your issue , you want to retain the original source values for the CreatedBy, ModifiedBy, CreateTime, and ModifiedTime columns during import. I have checked your code and you use the SPImportUserInfoDateTimeOption.ImportAll , so it should have had no problem. But you neglect one thing, which is that you operate on Import Object before import.Run(), so the CreateTime and ModifiedTime become the current time.

                    You could try to comment ‘import.Started += new EventHandler<SPDeploymentEventArgs>(import_Started);’ row and then test results.

     

                    Hope this could help you.

     

    Best Regards,

    Porter Wang

    Thursday, October 7, 2010 9:20 AM
  • Hi,

    Thanks for your response, i commented that line but didnt work. It still updates created and modified time to now() :(

    Regards,

    Seema

     

    Monday, October 11, 2010 6:04 AM
  • in my opinion , you cannnot import the create time and modify time.

    the create time and modify time is the time when the new item is created. so they are always the current time when you import items.


    SharePoint Gardener in China
    Monday, October 11, 2010 6:46 AM
  • Hi Karnek,

     

                    Ok, it’s a strange problem certainly as it works well on my machine. Please following the steps below to troubleshoot.

    1.       Create a new windows console application project.

    2.       Create a new and simple List, add some item to it.

    3.       Write your code into the console class ‘s Main method.

    Then, Run and debug your code to focus on the problem.

    Hope this could help you.

     

    Best Regards,

    Porter Wang

     

    Monday, October 11, 2010 8:37 AM
  • Hello,

    i tried in new console appln with above code. by commenting the line specified by you. But still didn t work :(

    if u have a sample code to export import listitem then please paste here.

    Regards,

    Seema

    Monday, October 11, 2010 11:05 AM
  • Hi Karnek,

     

                    My sample code is in much the same way as you, but it works fine. Here is it.

     

                    protected static SPSite siteCollection = new SPSite("http://porter:1130");

            //protected static SPSite siteCollection = new SPSite("http://porter");

            protected static SPWeb site = siteCollection.OpenWeb();

            static void Main(string[] args)

            {

                SPSecurity.RunWithElevatedPrivileges(delegate()

                {

                    using (siteCollection)

                    {

                        using (site)

                        {

    string exportFolder = @"C:\exports\";

                string exportFileName = "Export_File.cmp";

     

                SPList oList = site.Lists["www"];

                foreach (SPListItem item in oList.Items ) {

                    Console.WriteLine(item.ID);

                }

                SPListItem oItemA = oList.GetItemById(5);

     

                SPExportObject exportObject = new SPExportObject();

                exportObject.Id = oItemA.UniqueId;

                exportObject.Type = SPDeploymentObjectType.ListItem;

     

                SPExportSettings exportSettings = new SPExportSettings();

                exportSettings.LogExportObjectsTable = true;

                exportSettings.LogFilePath = exportFolder + "Exportlog.log";

                exportSettings.SiteUrl = "http://porter:1130";

                exportSettings.ExportMethod = SPExportMethodType.ExportAll;

                exportSettings.FileLocation = exportFolder;

                exportSettings.BaseFileName = exportFileName;

                exportSettings.ExportObjects.Add(exportObject);

                exportSettings.OverwriteExistingDataFile = true;

     

                exportSettings.ExcludeDependencies = true;

     

                SPExport export = new SPExport(exportSettings);

                export.Run();

     

                SPImportSettings settings = new SPImportSettings();

                settings.SiteUrl = "http://porter";

                settings.WebUrl = "http://porter";

                settings.FileLocation = exportFolder;

                settings.LogFilePath = exportFolder + "Importlog.log";

                settings.RetainObjectIdentity = false;

                settings.BaseFileName = exportFileName;

                settings.UserInfoDateTime = SPImportUserInfoDateTimeOption.ImportAll;

     

                SPImport import = new SPImport(settings);

                import.Run();

                        }

                    }

                });

     

            }

     

    Best Regards,

    Porter Wang

    • Proposed as answer by Arup Biswas Tuesday, October 12, 2010 6:22 AM
    • Marked as answer by KeFang Chen Friday, October 15, 2010 5:59 AM
    Tuesday, October 12, 2010 3:14 AM
  • Hello,

    i finally got the break through.

    When you had told me to create new document library , new console application , it had imported the timestamp correctly first time. I then deleted the same item from destination site and tried export and import again on same item. At this time, it didnt import timestamp correctly. So i thought its not consistent and could not know why its not importing second time. I deleted from recycle bin even then it didnt import the timestamp correctly. Then again new doc library works. So the conclusion is only once import works from a item.

    i dont know if its a limitaition of Moss 2007 that it can import only once.

    Thanks for all your support.

    Tuesday, October 12, 2010 12:08 PM