Domanda

Il seguente http://blogs.technet.com/b/stefan_Gossner/Archive/2007/08/30/Deep-dive-into-the-SharePoint-content-Deploy-e-migrazione-API-Part-4.Sagx fornisce il codice FOLLWoinF per importare ed esportare un elemento utilizzando la distribuzione del contenuto e l'API della migrazione.

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.Ho cambiato le seeting in modo che utilizzi i miei nomi di elenchi e il mio URL del sito.L'esportazione funziona alla grande.L'importazione non funziona.Sto eseguendo il codice su un documento lib.Non vengono lanciati errori ma l'elemento del contenuto non sta cambiando l'elenco.

Cosa potrebbe sbagliare?Anche se cancellato l'articolo orginale dopo l'esportazione, quindi eseguire l'importazione senza modificare la lista dei genitori nulla accade.

È stato utile?

Soluzione

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

        }
    }

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top