Friday 16 May 2008

Extending the functionality of the EXPORT Operation of STSADM

With this task it was important to understand the functionality of the current export operation to see whether it was possible to extend it or create a new functionality altogether.
I assumed that the items to be exported were lists, document libraries, blogs, task lists and the like. I tried to imagine having to export my blog from one portal to another.
SharePoint Content Deployment and Migration API
WSS and MOSS use this API at various different places:
* Content Deployment
* STSADM -o export and import
* Copy/Move operations in Site Manager
* Variations
* MCMS 2002 database migration to MOSS 2007
yy

The Content Deployment and Migration API provides the following features:

  • export an entire site collection
  • export a specific site inside a site collection including or excluding content in subsites
  • export a list or document libraries or even of a folder inside a document library
  • export a single list items or documents from a document library
  • export dependent objects (like images referenced by a page) by following links
  • generate as a compressed export file or in uncompressed format
  • allow export with a define a maximum size for the generated compressed file (multiple export files will be created if required)
  • allow incremental export of items based on a given change token. This will export all items that have been created, changed or deleted after the timestamp in the change token.
  • import the exported content with or without identiy preservation (means items will keep their GUID or not)
  • import the exported content under the same or a differnt parent in the destination database
  • do link fixup during import.

In particular we are interested in the use of a change token to export changes only made since the last export.

Programming Reference

Assembly: Microsoft.SharePoint.dll
Namespace: Microsoft.SharePoint.Deployment

Important Objects in the API:

  • SPExport - controls the Export process
  • SPExportSettings - used to configure the export process
  • SPExportObject - defines which objects need to be exported
  • SPImport - controls the import process
  • SPImportSettings - used to configure the import process

The SPExport Class supports exporting specified content from a source Windows SharePoint Services site collection to a cabinet (.cab) file in XML format. This class participates with other classes in the Deployment namespace to support importing, exporting, publishing, and migrating Windows SharePoint content, as well as supporting backup and restore capabilities.

** You can initiate an export operation by first initializing an instance of the Microsoft.SharePoint.Deployment.SPExportSettings class with the required export settings, and then passing the SPExportSettings object to the constructor of SPExport class; you then call the SPExport.Run method.

The following code example demonstrates how to perform an incremental export. Notice that the code sets the ExportMethod property to ExportChanges and then provides a change token.

settings.ExportMethod = SPExportMethodType.ExportChanges;
settings.ExportChangeToken = "1;1;87a71761-2987-48eb-9d29-48428270e01;632937036861200000;5512";

Reference: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.deployment.aspx

To export an entire site collection:

SPExportSettings settings = new SPExportSettings();
settings.SiteUrl = "http://localhost:2000";
settings.ExportMethod = SPExportMethodType.ExportAll;
settings.FileLocation = @"c:\export";
settings.FileCompression = false;
settings.CommandLineVerbose = true;
SPExport export = new SPExport(settings);
export.Run();

where a SPExportSettings object is created to define the general configuration settings for the export to be performed. As we did not select a specific object to export the configured site collection will be selected for export. Then the SPExport object is created based on the configured settings and the export is started by calling the Run method.

The settings being used in the code above:

  • SiteUrl - this property defines which site collection the export should use. All objects being exported always have to be in the same site collection. The Content Deployment and Migration API cannot access items in different site collections in a single operation.
  • ExportMethod - this property allows to define whether to perform an incremental export (value = ExportChanges) or everything (value = ExportAll). Be aware that ExportChanges would require to provide an Export Change Token in a separate property.
  • FileLocation - this property defines where to store the exported content. The value should point to be an empty directory. If the directory does not exist it will be created during export. If file compression is being used, then only the compressed files will be stored on this location. The uncompressed files will be stored in the directory identified by the value of the system wide TMP environment variable. So you need to ensure that the directory the TMP environment variable points to also needs to have sufficient space available.
  • FileCompression - this property defines whether the content should be compressed into a CAB file. If you need to archive the exported content or need to transfer it to a different machine you should choose to compress. If you only export the content to import it afterwards using code on the same machine and don't need to archive (e.g. a copy or move operation) then you should decide to disable the compression as this is significantly quicker.
  • CommandVerbose - this parameter allows to control if the API should provide some verbose output. If you have ever seen the generated output when running STSADM -o export: this is exactly the flag the generates this output. If the value is false no output is generated.

To export specific items like lists, document libraries, list items or documents from a document library:

SPSite site = new SPSite("http://localhost:2000");
SPWeb web = site.OpenWeb("/SomeWeb");
SPList list = web.Lists["MyList"];
SPListItem listItem = list.Items[0]; // select the first list item

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

SPExportObject exportObject = new SPExportObject();
exportObject.Id = listItem.UniqueId;
exportObject.Type = SPDeploymentObjectType.ListItem;

where the object type will change as per the type you are trying to export.

To export incremental items
If incremental export should be done it is required to save the Change Token of the last full or incremental export. This change token needs then be provided in the ExportSettings to allow the Content Deployment and Migration API to determine which items have changed as follows:
SPExportSettings settings = new SPExportSettings();
...
SPExport export = new SPExport(settings);
export.Run();
string ChangeToken = settings.CurrentChangeToken;
CurrentChangeToken is a read-only parameter populated during export. It contains the Change Token right after the export. So doing an incremental export providing this change token in the future will export all items that have been created, changed or deleted in the configured scope after the change token was generated.
The following code implements an export
SPExportSettings settings = new SPExportSettings();
settings.ExportMethod = SPExportMethodType.ExportChanges;
settings.ExportChangeToken = oldChangeToken;
...
  • ExportMethod - this property allows to define whether to perform an incremental export (value = ExportChanges) or everything (value = ExportAll).
  • ExportChangeToken - this property defines which items to export when using incremental deployment. the incremental export will only export items that have been created, changed or deleted in the configured scope after the change token was generated.
Anothwer way to extend the functionality of stsadm is to create a class that implements the ISPStsadCommand
although i had trouble finding a command to export only items changed form the last export!
References:

http://technet.microsoft.com/en-us/library/cc262759.aspx an explanation of the stsadm operation

http://technet.microsoft.com/en-us/library/cc263384.aspx stsadm operations out of the box

http://blogs.technet.com/stefan_gossner/archive/2007/08/29/deep-dive-into-the-sharepoint-content-deployment-and-migration-api-part-2.aspx

http://www.aisto.com/roeder/dotnet/ This is one magical tool I would recommend everyone use. Reflector is the class browser, explorer, analyzer and documentation viewer for .NET. Reflector allows to easily view, navigate, search, decompile and analyze .NET assemblies in C#, Visual Basic and IL. just install it and piiunt it to stsadm or any program and it will break down every single class, attribute, method and operations for the program!!!

http://www.andrewconnell.com/blog/articles/MossStsadmWcmCommands.aspx

http://msdn.microsoft.com/en-us/library/aa367988.aspx Command Line parameters

http://msdn.microsoft.com/en-us/library/aa979099.aspx The sharepoint Deployment object model

No comments:

Zootmastaflex

Zootmastaflex
The Queen of RockStars!