Question

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.

Was it helpful?

Solution

I have tested your code and found the problem was with the files being created during exported were not cleaned up properly on the local disk. I implemented the files deletion logic to ensure Export and Import works fine. Note, I ran the Export and Import with and without FileCompression and both are working fine. I ran my test on two Picture libraries.

Sorry, I didn't have much time to improve your code more for e.g. properly disposing the SPWeb and SPSite objects. Hope you will do it yourself.

The updated code is pasted below:

class Program
    {
        static void Main(string[] args)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                // implementation details omitted
                SPSite site = new SPSite("http://mylocaldev/");
                SPWeb web = site.OpenWeb("/");
                SPList list = web.Lists["ParentPic"];
                SPListItem listItem = list.Items[0];

                try
                {
                    //delete the old files
                    DirectoryInfo dir = new DirectoryInfo(@"c:\export\CopyListItem");
                    foreach (System.IO.FileInfo file in dir.GetFiles()) file.Delete();                    

                    // export                        
                    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.BaseFileName = "exportfile.cmp";

                    exportSettings.SiteUrl = "http://mylocaldev/";

                    SPExport export = new SPExport(exportSettings);

                    export.Run();

                }
                catch (Exception ex)
                {

                }
                finally { web.Dispose();}


                // import

                SPImportSettings importSettings = new SPImportSettings();

                importSettings.SiteUrl = "http://mylocaldev/";

                importSettings.FileLocation = @"c:\export\CopyListItem";

                importSettings.FileCompression = false;

                //importSettings.BaseFileName = "exportfile.cmp";

                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://mylocaldev/");

            SPWeb web = site.OpenWeb("/");

            SPList list = web.Lists["ChildPic"];

            SPImportObjectCollection rootObjects = args.RootObjects;

            foreach (SPImportObject io in rootObjects)
            {

                io.TargetParentUrl = list.RootFolder.ServerRelativeUrl;

            }

            web.Dispose();

            site.Dispose();

        }
    }

OTHER TIPS

Fortunately, just before some days i was working on similar kind of tasks.

I have tried with your given code. and almost Its working.

But I am agree with @Falak that you should write code with proper try/catch and with SharePoint standard coding pattern.

Here, i found that once your code should run perfectly, second time you should get error, because you are not cleaning your File Location folder which is: "c:\export\CopyListItem".

This may be help to you.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top