Archiving list items is very important, because SharePoint has performance issues with large lists. So you have to execute archiving job from time to time to move unused (closed) items to another list (archive list). But moving items from one list to another is not so simple, because SharePoint does not have built-in function for that. But you can use SPExport and SPImport classes to implement this operation. The following is the class, I wrote for archiving items (ArchiveItem method moves item to another list):
class ArchiveManager
{
private SPSite _site;
private SPList _archiveList;
private SPWeb _sourceWeb;
public void ArchiveItem(SPListItem item, SPList archiveList)
{
_site = item.Web.Site;
_archiveList = archiveList;
_sourceWeb = item.Web;
SPExportSettings exportSettings = new SPExportSettings();
exportSettings.SiteUrl = _site.Url;
exportSettings.IncludeVersions = SPIncludeVersions.All;
exportSettings.IncludeSecurity = SPIncludeSecurity.None;
exportSettings.OverwriteExistingDataFile = true;
exportSettings.AutoGenerateDataFileName = true;
exportSettings.FileCompression = false;
exportSettings.ExportMethod = SPExportMethodType.ExportAll;
exportSettings.CommandLineVerbose = false;
SPExportObject obj = new SPExportObject();
obj.Id = item.UniqueId;
obj.Type = SPDeploymentObjectType.ListItem;
exportSettings.ExportObjects.Add(obj);
using (SPExport export = new Microsoft.SharePoint.Deployment.SPExport(exportSettings))
{
export.Run();
}
SPImportSettings importSettings = new SPImportSettings();
importSettings.SiteUrl = _site.Url;
importSettings.FileLocation = exportSettings.FileLocation;
importSettings.UpdateVersions = SPUpdateVersions.Append;
importSettings.IncludeSecurity = SPIncludeSecurity.None;
importSettings.FileCompression = false;
importSettings.UserInfoDateTime = SPImportUserInfoDateTimeOption.ImportAll;
importSettings.RetainObjectIdentity = false;
importSettings.CommandLineVerbose = false;
using (SPImport import = new SPImport(importSettings))
{
import.Started += new EventHandler
import.ObjectImported += new EventHandler
import.Run();
Directory.Delete(import.Settings.FileLocation, true);
}
}
protected void OnImportStarted(object sender, SPDeploymentEventArgs e)
{
if (e.RootObjects != null)
{
ReparentImportObjects(e.RootObjects);
}
}
private void ReparentImportObjects(SPImportObjectCollection importObjects)
{
foreach (SPImportObject importObject in importObjects)
{
importObject.TargetParentUrl = _archiveList.RootFolder.ServerRelativeUrl;
}
}
private void OnObjectImported(object sender, SPObjectImportedEventArgs e)
{
if (e.Type == SPDeploymentObjectType.ListItem)
{
object obj = _sourceWeb.GetObject(e.SourceUrl);
if (obj is SPListItem)
{
SPListItem item = obj as SPListItem;
ReplaceBackLinks(item.BackwardLinks, _site, e.SourceUrl, e.TargetUrl);
item.Recycle(); //delete item from source list
}
}
}
private void ReplaceBackLinks(SPLinkCollection links, SPSite site, string source, string target)
{
if (links == null)
return;
int count = links.Count;
for (int i = count - 1; i >= 0; i--)
{
SPLink link = links[i];
using (SPWeb rweb = site.OpenWeb(link.ServerRelativeUrl, false))
{
object o = rweb.GetObject(link.ServerRelativeUrl);
if (o is SPFile)
{
SPFile f = o as SPFile;
f.ReplaceLink(source, target);
}
if (o is SPListItem)
{
SPListItem l = o as SPListItem;
l.ReplaceLink(source, target);
}
}
}
}
}

Wonderful website. A lot of useful info here. I am sending it to several buddies ans also sharing in delicious. And certainly, thanks to your effort!
Hi there, You’ve performed a fantastic job. I’ll definitely digg it and personally recommend to my friends. I am confident they will be benefited from this website.
Hello,
Unfortunately, I have never tried this code with custom task list and did not experience the same problems with that. I guess the reason is not in WF associations, because I’ve tried this code on the list with WF and it worked fine.
Do you get any errors during import? Check event log and SharePoint log.