Content Migration APIs provides a solution for migrating contents between the WSS sites.These APIs are designed on the Object model and Microsoft.Sharepoint.Deployment is the core namespace used to perform the task.
We may come across a situation where we need to move the contents from one sharepoint site to the other eg : Moving all the list items from one site to the other . I was asked to build a utility to migrate and update the data b/w the portals,with these APIs i could build a utility that does the task.
Using this model involves 2 steps :
1.Exporting the contents.
2.Importing the contents and restoring them in the import server site.
Now ,Lets see each one of these sections in detail
1.Exporting the Contents -We can export the objects at different levels,which includes :
Export the whole site collection
Export the sub site.
Export the list or its items.
SPSite Src_Site = new SPSite(Your Export site);
SPWeb Src_Web = Src_Site.AllWebs[“Test”];
SPExportObject Expobj = new SPExportObject();
Expobj.Type = SPDeploymentObjectType. * ;
SPExportSettings Exp_Settings = new SPExportSettings();
Exp_Settings.SiteUrl = Src_Web.Url;
Exp_Settings.BaseFileName = “Sample.cmp”;
Exp_Settings.FileLocation = @”C:\”; //Generates Sample.cmp at the location C:\ .
SPExport Export = new SPExport(Exp_Settings);
Objects Used –
SPExportObject – This object specifies the objects that could be exported, may be like entire site coll ,sub site ,list or the list items. * in the code above indicates the objects that we could export. We will get the intellisense there which is self-explanatory.
SPExportSettings -This object contains all the config settings for the export operations.
SPExport -This object performs the export operation, it calls the Run() to instantiate the export process.
On sucessfull export, the “Sample.cmp” file would be created at the location C:\(See the code above). Export process creates a CMP package called Content Migration Package(CMP) which contains all the items that needs to be restored in the import server.
2.Importing the contents – Now,lets see how to import these exported site contents and restore them in yet another WSS site. We can import the contents in either of the 2 ways,
By preserving the object identity – This means we retain the URL and GUID of the items we import as it is,which means the parent for such items remains the same even after importing. To do this we need to set the RetainObjectIdentity property of the SPImportSettings object to “true”.
SPSite Dest_Site = new SPSite(Your Import site);
SPWeb Des_Web = Dest_Site.AllWebs[“Sample”];
SPImportSettings Imp_Settings = new SPImportSettings();
Imp_Settings.SiteUrl = Des_Web.Url;
Imp_Settings.BaseFileName = “Sample.cmp”;
Imp_Settings.FileLocation = @”C:\”;
Imp_Settings.RetainObjectIdentity = true;
SPImport import = new SPImport(Imp_Settings);
Objects Used –
SPImportSettings –This object contains all the Config settings for the Import operations.
SPImport – This object performs the import operation, it calls the Run() to instantiate the import process.
By not preserving the Object Identity – This is yet another approach for importing the objects those have been exported already. In this approach we import object with different GUID ,which is done by setting the value “false” for the RetainObjectIdentity property of the SPImportSettings object, such objects which are imported with a different GUID needs new parent to be created otherwise,the objects will become orphaned.The import operation takes the CMP created by the export operation to restore the contents in the import server.
The new parent has to be created ,once the list items have been successfully imported from the package.To achieve this we will be using the ObjectImported event handler.
static void ObjectImported(object sender, SPDeploymentEventArgs args)
SPSite MySite = new SPSite(“Site on the import server”);
SPWeb MyWeb = MySite .RootWeb;
SPImportObjectCollection ObjColl= args.RootObjects;
foreach (SPImportObject io in ObjColl)
io.TargetParentUrl = MyWeb .Url;
Now,we should be able to see the contents moved between the 2 WSS Sites.This approach however,cannot export or import configuration settings,alerts,custom web parts,workflows and other customizations.