SharePoint offers a lot of out-of-box features for managing content from an end user perspective. Sometimes you may want your end users to have the capability to migrate content across multiple site collections, or even different web applications. In my case, I needed a solution which would allow a user to publish a document, list, library, etc to a site external of the current site collection. The solution for this was to build a custom feature utilizing Microsoft SharePoint Deployment.
The deployment namespace allows you to package your content into a .cmp file and perform export/import operations, very similar to the concept behind doing so for a webpart through the UI.
Transform your business operations with our Microsoft solutions
Explore Our Microsoft Consulting Services →
SPExportSettings mySettings = new SPExportSettings();
mySettings.SiteUrl = “http://mysharepointsite”;
mySettings.ExportMethod = SPExportMethodType.ExportAll;
mySettings.FileLocation = “d:tempexportlocation”;
mySettings.FileCompression = false;
SPExport export = new SPExport(mySettings);
myWeb.AllowUnsafeUpdates = true;
myWeb.Site.WebApplication.FormDigestSettings.Enabled = false;
myWeb.AllowUnsafeUpdates = false;
myWeb.Site.WebApplication.FormDigestSettings.Enabled = true;
SPSite readerSite = new SPSite(“http://sharepointreadersite”);
SPWeb readerWeb = readerSite.OpenWeb();
SPWebApplication webApp = readerWeb.Site.WebApplication;
SPImportSettings settings = new SPImportSettings();
settings.SiteUrl = “http://sharepointreadersite”;
settings.FileLocation = “d:tempexportlocation”;
settings.FileCompression = false;
settings.RetainObjectIdentity = false;
SPImport import = new SPImport(settings);
webApp.FormDigestSettings.Enabled = false;
projectsWeb.AllowUnsafeUpdates = true;
Something noteworthy here is that I exported the .cmp file to a location on the server. In my case, the export and import web applications were both hosted on the same server. You’ll want to be aware of this and make sure that wherever you place this .cmp file that both of your SharePoint sites have access to the location. Also, I made a practice of deleting this file from the export location after an import attempt, without regard to whether the import was successful or not.
Your export object can range from a single file to an entire document library, though this will need to be defined in your export object properties for this export/import to function successfully.