The following http://blogs.technet.com/b/stefan_gossner/archive/2007/08/30/deep-dive-into-the-sharepoint-content-deployment-and-migration-api-part-4.aspx provides the follwoinf code to import and export an item using the Content Deployment and Migration API.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Deployment;
namespace WssOMtest
{
class Program
{
static void Main(string[] args)
{
// export
SPSite site = new SPSite("http://localhost:4000");
SPWeb web = site.OpenWeb("/");
SPList list = web.Lists["MyList"];
SPListItem listItem = list.Items[0];
SPExportObject exportObject = new SPExportObject();
exportObject.Id = listItem.UniqueId;
exportObject.Type = SPDeploymentObjectType.ListItem;
SPExportSettings exportSettings = new SPExportSettings();
exportSettings.ExportObjects.Add(exportObject);
exportSettings.FileLocation = @"c:\export\CopyListItem";
exportSettings.FileCompression = false;
exportSettings.SiteUrl = "http://localhost:4000";
SPExport export = new SPExport(exportSettings);
export.Run();
web.Dispose();
// import
SPImportSettings importSettings = new SPImportSettings();
importSettings.SiteUrl = "http://localhost:4000";
importSettings.FileLocation = @"c:\export\CopyListItem";
importSettings.FileCompression = false;
importSettings.RetainObjectIdentity = false;
SPImport import = new SPImport(importSettings);
EventHandler<SPDeploymentEventArgs> startedEventHandler = new EventHandler<SPDeploymentEventArgs>(OnStarted);
import.Started += startedEventHandler;
import.Run();
// cleanup
site.Dispose();
}
// event handler to assign a new parent to the orphaned image in the package
static void OnStarted(object sender, SPDeploymentEventArgs args)
{
SPSite site = new SPSite("http://localhost:4000");
SPWeb web = site.OpenWeb("/PressReleases");
SPList list = web.Lists["TargetList"];
SPImportObjectCollection rootObjects = args.RootObjects;
foreach (SPImportObject io in rootObjects)
{
io.TargetParentUrl = list.RootFolder.ServerRelativeUrl;
}
web.Dispose();
site.Dispose();
}
}
}
Ok great. I have changed the seetings so it uses my list names and my site url. The export is working great. The import is not working. I am performing the code on a document lib. No errors are thrown but the content item is not changing list.
What could be going wrong? Even if i delete the orginal item after the export then do the import without changing the parent list nothing happens.