3

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.

1
  • So the list item you're exporting is not imported into the target list, is that right? Does your target list include the same content types as the source list? Also, are there any messages in ULS which might indicate what's going on there?
    – Jasper
    Commented Jul 11, 2012 at 10:10

2 Answers 2

3
+50

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();

        }
    }
2
  • Thanks will try it out. Cleaning up the code is fine. I used the code Stefan Goßner provided. I prefer using "using" blocks.
    – John
    Commented Jul 12, 2012 at 7:12
  • I would suggest you using Try, Catch and Dispose the SPWeb/SPSite object in Finally statement. Commented Jul 12, 2012 at 9:27
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.