SharePoint content migration using the content migration object model

Following on from a previous post, this post will focus on the SharePoint content migration object model.

For content migration scenarios that require you to export only selected content, or that require you to automate or customize migration operations, there is only one approach: you must write a custom solution that uses the APIs in the Microsoft.SharePoint.Deployment namespace.

It is important that before you choose to use selective migration that you have performed a full content migration first, so that the destination is a mirror image of the source.

Selective migration applies, typically, to content that needs to be migrated from server to server based on factors such as content version (current versus future), time stamp, and content state (approved versus in review, for example). Selection criteria provides a high degree of granularity from the scope of the site collection down; that is, you have selection control at the scope of the Web, the list, folder, and list item.

You can have any number of content management packages (.cmp) files in a migration operation, and you can also have multiple destinations. However, the objects contained in a given .cmp file must originate from a single site collection.

Had enough of the pre-amble? Lets move on to how you can start using the SharePoint contnect migration object model.

  1. Reference Microsoft.SharePoint.dll
    This can normally be located in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\Microsoft.SharePoint.dll
  2. Include the following Microsoft.Sharepoint.dll namespaces
    Add these using statements to the top of your class:

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Deployment;
  3. To export data to a content management package, adapt the following code to your requirements
    static void Export()
    {
    // Full content migration
    string siteURL = “<Your SharePoint site URL>”
    string baseFileName = “<Your content package name>.cmp”;
    string dataFileLocation = @”c:\”;
    string logFileLocation = @”c:\<Your export log file name>.log”;

    SPExportSettings exportSettings = new SPExportSettings();
    exportSettings.SiteUrl = siteURL;
    exportSettings.ExportMethod = SPExportMethodType.ExportAll;
    exportSettings.BaseFileName = baseFileName;
    exportSettings.FileLocation = dataFileLocation;
    exportSettings.LogFilePath = logFileLocation;
    exportSettings.OverwriteExistingDataFile = true;
    exportSettings.CommandLineVerbose = true;
    SPExport export = new SPExport(exportSettings);
    export.Run();
    }

  4. Export selective content

    To export selective content add as many SPExportObjects to the SPExportSettings’ collection.

    As SharePoint objects are identified by GUID on the source server. You will need to find out the GUID of the object you wish to export first.

    Using a SPList as an example:

    string siteURL = “<Your SharePoint site URL>”
    SPSite systalkSite = new SPSite(siteURL);
    SPList mySelectiveContent= systalkSite.AllWebs[0].Lists[<Your selected list name>];

    SPExportObject exportObject = new SPExportObject();
    exportObject.Id = mySelectiveContent.ID;
    exportObject.Type = SPDeploymentObjectType.List;

    exportSettings.ExportObjects.Add(exportObject);

  5. To import data from a content management package, adapt the following code to your requirements

    static void Import()
    {
    // Package import
    System.Uri siteURL = new Uri(“”<Your SharePoint site URL>””);
    string baseDataFileName = “<Your content package name>.cmp”;
    string dataFileLocation = @”c:\”;
    string logFileLocation = @”c:\<Your export log file name>.log”;

    SPImportSettings importSettings = new SPImportSettings(siteURL, dataFileLocation, baseDataFileName);
    importSettings.RetainObjectIdentity = true;
    importSettings.CommandLineVerbose = true;
    importSettings.LogFilePath = logFileLocation;
    SPImport import = new SPImport(importSettings);
    import.Run();

    }

Theres a lot of scope for customisation using this approach, but these examples should be enough to get you started.

Further reading:
Key Concepts in Selective Migration

Selective Content Migration in Windows SharePoint Services 3.0

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: