Pregunta

LO SIGUIENTE http://blogs.technet.com/b/stefan_gossner/archive/2007/08/30/DEEP-dive-into-the-sharepoint-content-deSployy la migración-API-PART-4.ASPX proporciona el código FOLLWOINF para importar y exportar un elemento utilizando la implementación de contenido y la API de migración.

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 GRAN.He cambiado las heetsings para que use los nombres de mis listas y la URL de mi sitio.La exportación está funcionando muy bien.La importación no está funcionando.Estoy realizando el código en un documento lib.No se lanzan errores, pero el elemento de contenido no está cambiando.

¿Qué podría estar equivocado?Incluso si eliminan el artículo orginal después de que la exportación se realice la importación sin cambiar la lista de padres, no pasa nada.

¿Fue útil?

Solución

He probado su código y he encontrado que el problema fue que los archivos que se crean durante los exportados no se limpiaron correctamente en el disco local.Implementé la lógica de eliminación de archivos para garantizar que la exportación e importación funcione bien.Nota, corrí la exportación e importación con y sin FileCompression y ambos están funcionando bien.Corrí mi prueba en dos bibliotecas de imágenes.

Lo siento, no tuve mucho tiempo para mejorar su código más para, por ejemplo.Desechando adecuadamente los objetos SPWEB y SPSITE.Espero que lo hagas tú mismo.

El código actualizado se pega a continuación:

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

        }
    }

Otros consejos

Afortunadamente, justo antes de algunos días trabajando en un tipo similar de tareas.

He intentado con su código dado.y casi su trabajo.

Pero estoy de acuerdo con @falak que debe escribir código con la prueba / captura de intento adecuada y con el patrón de codificación estándar de SharePoint.

Aquí, descubrí que una vez que su código se ejecute perfectamente, la segunda vez debe obtener un error, ya que no está limpiando su carpeta Ubicación del archivo , que es: " c: \ export \Copilistitem ".

Esto puede ser de ayuda para usted.

Licenciado bajo: CC-BY-SA con atribución
scroll top