Contact

Technology

Mar 25, 2009

MOSS Content Deployment

Credera Team

Credera Team

Default image background

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.

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

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  →

[code lang=”c#”]

//export

SPExportSettings mySettings = new SPExportSettings();

mySettings.SiteUrl = “http://mysharepointsite”;

mySettings.ExportMethod = SPExportMethodType.ExportAll;

mySettings.FileLocation = “d:tempexportlocation”;

mySettings.FileCompression = false;

mySettings.ExportObjects.Add(myExportObject);

SPExport export = new SPExport(mySettings);

myWeb.AllowUnsafeUpdates = true;

myWeb.Site.WebApplication.FormDigestSettings.Enabled = false;

myWeb.Update();

export.Run();

myWeb.AllowUnsafeUpdates = false;

myWeb.Site.WebApplication.FormDigestSettings.Enabled = true;

myWeb.Update();

//import

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;

projectsWeb.Update();

import.Run();

[/code]

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.

Conversation Icon

Contact Us

Ready to achieve your vision? We're here to help.

We'd love to start a conversation. Fill out the form and we'll connect you with the right person.

Searching for a new career?

View job openings