none
Programmatically move Discussion from one Discussion Board List to another RRS feed

  • Question

  • Can anyone help me for the code to programmatically move "Discussion" with all its replies(without changing created by and modified by) from one Discussion Board List to another. I tried using splistitem.MoveTo() or CopyTo...but it doesn't seems to work.

     

    Thanks in advance

     

    Wednesday, May 25, 2011 4:41 AM

Answers

  • Finally it worked for me. Here is the Code for the same

    SPExportObject exportObject = new SPExportObject();
              exportObject.Id = folder.UniqueId;
              exportObject.Type = SPDeploymentObjectType.Folder;
              exportObject.IncludeDescendants = SPIncludeDescendants.All;//to import all list items under the current folder
    
              SPExportSettings exportSettings = new SPExportSettings();
              exportSettings.LogExportObjectsTable = true;
              exportSettings.LogFilePath = EXPORTFILEPATH + "Exportlog.log";
              exportSettings.SiteUrl = currentWeb.Site.Url;
              exportSettings.ExportMethod = SPExportMethodType.ExportAll;
              exportSettings.FileLocation = EXPORTFILEPATH;
              exportSettings.BaseFileName = EXPORTFILENAME;
              exportSettings.ExportObjects.Add(exportObject);
              exportSettings.OverwriteExistingDataFile = true;
              exportSettings.IncludeSecurity = SPIncludeSecurity.All;
              exportSettings.IncludeVersions = SPIncludeVersions.All;
              exportSettings.ExcludeDependencies = true;
    
              SPExport export = new SPExport(exportSettings);
              export.Run();
    
              SPImportSettings settings = new SPImportSettings();
              settings.SiteUrl = currentWeb.Site.Url;
              settings.WebUrl = currentWeb.Url;
              settings.FileLocation = EXPORTFILEPATH;
              settings.LogFilePath = EXPORTFILEPATH + "Importlog.log";
              settings.RetainObjectIdentity = false;
              settings.BaseFileName = EXPORTFILENAME;
              settings.UserInfoDateTime = SPImportUserInfoDateTimeOption.ImportAll;
    
              SPImport import = new SPImport(settings);
              EventHandler<SPDeploymentEventArgs> startedEventHandler = new EventHandler<SPDeploymentEventArgs>(OnStarted);
              import.ObjectImported += new EventHandler<SPObjectImportedEventArgs>(import_ObjectImported);
              import.Started += startedEventHandler;
              import.Run();
    
    void OnStarted(object sender, SPDeploymentEventArgs args)
        {
          SPSite site = null;
          SPWeb web = null;
          try
          {
            site = new SPSite(strTargetWebUrl);
            web = site.OpenWeb();
            web.AllowUnsafeUpdates = true;
            SPList list = WSSHelper.GetList(strTargetList, web);
            SPImportObjectCollection rootObjects = args.RootObjects;
            foreach (SPImportObject io in rootObjects)
            {
              io.TargetParentUrl = list.RootFolder.ServerRelativeUrl;
            }
          }
          catch (Exception ex)
          {
            
          }
          finally
          {
            web.AllowUnsafeUpdates = false;
            web.Dispose();
            site.Dispose();
          }
        }
    
    
    
    
    
    



     

     

     

    Wednesday, August 3, 2011 5:04 AM

All replies

  • Hi Anna Jhaveri,

     

    For your issue, I’m not sure if you want to migrate all list items to another list. If so, you could try to make use of splist.saveastempate() method to fix it. Please refer to this thread:

    http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/139e54de-bfa3-47b2-a56b-641830f76a0c

     

    If not, you just want to move some listitems, you could take into account the SPExportObject and SPImportObject. Please refer to:

    http://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/aa1fc590-adbd-41a4-96b3-0c218e11eb69

     

    Thanks,

    Porter Wang

    Friday, May 27, 2011 6:03 AM
  • Hello,

    I want to move particular one discussion from one discussion board list to another. As Discussions are stored as folders and its replies are stored as list item inside those folder , I am not clear about how to move SPFolder with all list items stored in it using SPExportSetting...

    Any help is appreciated..

    Thanks in advance

    Saturday, May 28, 2011 4:21 AM
  • Finally it worked for me. Here is the Code for the same

    SPExportObject exportObject = new SPExportObject();
              exportObject.Id = folder.UniqueId;
              exportObject.Type = SPDeploymentObjectType.Folder;
              exportObject.IncludeDescendants = SPIncludeDescendants.All;//to import all list items under the current folder
    
              SPExportSettings exportSettings = new SPExportSettings();
              exportSettings.LogExportObjectsTable = true;
              exportSettings.LogFilePath = EXPORTFILEPATH + "Exportlog.log";
              exportSettings.SiteUrl = currentWeb.Site.Url;
              exportSettings.ExportMethod = SPExportMethodType.ExportAll;
              exportSettings.FileLocation = EXPORTFILEPATH;
              exportSettings.BaseFileName = EXPORTFILENAME;
              exportSettings.ExportObjects.Add(exportObject);
              exportSettings.OverwriteExistingDataFile = true;
              exportSettings.IncludeSecurity = SPIncludeSecurity.All;
              exportSettings.IncludeVersions = SPIncludeVersions.All;
              exportSettings.ExcludeDependencies = true;
    
              SPExport export = new SPExport(exportSettings);
              export.Run();
    
              SPImportSettings settings = new SPImportSettings();
              settings.SiteUrl = currentWeb.Site.Url;
              settings.WebUrl = currentWeb.Url;
              settings.FileLocation = EXPORTFILEPATH;
              settings.LogFilePath = EXPORTFILEPATH + "Importlog.log";
              settings.RetainObjectIdentity = false;
              settings.BaseFileName = EXPORTFILENAME;
              settings.UserInfoDateTime = SPImportUserInfoDateTimeOption.ImportAll;
    
              SPImport import = new SPImport(settings);
              EventHandler<SPDeploymentEventArgs> startedEventHandler = new EventHandler<SPDeploymentEventArgs>(OnStarted);
              import.ObjectImported += new EventHandler<SPObjectImportedEventArgs>(import_ObjectImported);
              import.Started += startedEventHandler;
              import.Run();
    
    void OnStarted(object sender, SPDeploymentEventArgs args)
        {
          SPSite site = null;
          SPWeb web = null;
          try
          {
            site = new SPSite(strTargetWebUrl);
            web = site.OpenWeb();
            web.AllowUnsafeUpdates = true;
            SPList list = WSSHelper.GetList(strTargetList, web);
            SPImportObjectCollection rootObjects = args.RootObjects;
            foreach (SPImportObject io in rootObjects)
            {
              io.TargetParentUrl = list.RootFolder.ServerRelativeUrl;
            }
          }
          catch (Exception ex)
          {
            
          }
          finally
          {
            web.AllowUnsafeUpdates = false;
            web.Dispose();
            site.Dispose();
          }
        }
    
    
    
    
    
    



     

     

     

    Wednesday, August 3, 2011 5:04 AM