Exporting and importing SharePoint 2007 content using the object model

By using the SharePoint object model you can export several types of content to disk.You can do this by setting up a number of SPExportObject objects. These can be added to a SPExportSettings object. The last step is to create a SPExport object and call it's Run method. This is a very easy way to store content from a SharePoint site on disk and move it to another site collection, or another MOSS server/farm. In fact, MOSS Content Deployment uses the same objects.
Example 1 - Export a site
The first example shows you how to export a site to a file. The site exported in my sample is a subsite of a "Collaboration Portal" top level site. This is a site called “ContentDeploymentTest” and is has relative url “cdt”. The first step is to create and setup the SPExportObject. By setting the “Type” property of this object, you determine what content you are going to export. At this stage, the SDK is not very clear on what all options mean, but most values are pretty clear:
  • File
  • Folder
  • List
  • ListItem
  • Site
  • Web
I wanted to export just 1 site (including sub sites), so I used SPDeploymentObjectType.Web. I am not exactly sure what the IncludeDescendants option is for (here is the SDK link), but it has these options to choose from:
  • All
  • Content
  • None
The second object to create is SPExportSettings. The last 2 parameters of the constructor let you decide where to export the export the objects to. In my case, the site gets stored in a file called “SPExportTest.cab” in the folder “c:\ExportWebs”. If you don’t set FileCompression to true, the files are not packaged into one single cab package. An important thing to do in this step is add the SPExportObject (or multiple) we just created.
The last step is to create an SPExport object. This takes the SPExportSettings object we just created as a parameter for the constructor. After doing this we call the Run() method and we are ready to test our export!
    string sourceUrl = "http://office2007/cdt";
 
    // Create the export object
    SPExportObject exportSite = new SPExportObject();
    exportSite.Type = SPDeploymentObjectType.Web;
    exportSite.Url = sourceUrl;
    exportSite.ExcludeChildren = false;
    exportSite.IncludeDescendants = SPIncludeDescendants.All;
 
    // Create the export settings
    SPExportSettings settings = new SPExportSettings(
        new Uri("http://office2007"), @"c:\ExportWebs", "SPExportTest.cab");
    settings.ExportObjects.Add(exportSite);
    settings.LogFilePath = @"c:\ExportWebs.log";
    settings.FileCompression = true;
 
    // Create the export object and run the export
    SPExport export = new SPExport(settings);
    export.Run();
After running this example, you end up with a logfile in the root of the C-drive and a cab file. If you open the cab file, you will see a lot of “.DAT” files and a number of XML files. The DAT files are all the pages and documents in your site. These are not only the pages and documents you created, but also include all SharePoint ASPX pages (like upload.aspx, webfldr.aspx, etc.) The manifest.xml file is the index for these files.
This is slightly different from a Personal Web Package that you can create by using SharePoint Designer. If you export a site to a Personal Web Package, you also end up with a cab file (with extension FWP). This cab file has 1 xml file called manifest.xml.  Apart from that you will see a lot of ASPX files and all the documents you uploaded. All files are named like “”file0002.aspx””, and the manifest file contains the index.
Example 2 - Import a site
Example 1 resulted in a cab file with a dump of one of my sites. Importing the into another site collection is very easy. The only thing that you have to take care of is making sure that there is a site with the correct url in your site collection. In my example I had to create a site with url “cdt” in my site collection (at url http://office2007:90). The reason for this manual step is the fact that I decided to export a subsite of my top level site, instead of exporting the whole top level site.
This process is very easy. First you setup an SPImportSettings object and point that to the destination site collection and to the location of the CAB file. After that you setup a SPImport object and call the Run method.
    string destinationUrl = "http://office2007:90";
 
    // Create the import settings
    SPImportSettings settings = new SPImportSettings(
        new Uri(destinationUrl), @"c:\ExportWebs", "SPExportTest.cab");
    settings.LogFilePath = @"c:\ImportWebs.log";
    settings.FileCompression = true;
 
    // Create the import object and run the import
    SPImport import = new SPImport(settings);
    import.Run();
An important thing to realize is that by using this way to export and import content, the source and the destination site will always have the same site structure. If you point the SPImportSettings to a different subsite of your site collection, it will still import the content to the original location. This is because the manifest.xml contains the relative links to the items (documents, sites, pages) from the root of the top level site.
The next post will show how you can automate the setup of content deployment, that uses the same objects as I just discussed here.

No comments:

Post a Comment