Overview

In many deployment life cycles, SharePoint is built in a pilot environment and then after some iteration of tests, it is time to move it to production. Some companies elect to trash the pilot environment, while others need to take the data from their Pilot environment and move it to Production. SharePoint 2010 offers many ways to accomplish this task that were not available to us in the 2007 release. PowerShell has helped open the door in both products to move our data from one environment to another.

In this article, we will take a site and move it into another environment. You may be referring to your destination as Dev, Staging, Production, or a number of other common terms; nonetheless, PowerShell is a powerful tool that can take the Admin out of administration.

If you use the standard exportimport using STSADM for SharePoint 2007, you will lose the Meta data associated with each of the items. This script preserves this data, allowing you to maintain the data.

While the examples provided where written for SharePoint 2007, the API used is also supported in 2010.  SharePoint 2010 comes with a new cmdlet name Export-SPWeb that is used to export sites and lists, so hopefully this code won’t be needed.

The API

SPExportSettings properties can be reviewed at http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.deployment.spexportsettings_properties.aspx

SPImportSettings properties can be review at http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.deployment.spimportsettings_properties.aspx

The Export Script

# Specify the properties of the web site that will be exported

$siteURL = http://ps-moss:11030/”

$webName = “test”

$fileName = “exportedWeb.cmp”

$fileLocation = “c:export”

# Load the Microsoft.SharePoint DLL

[void][system.reflection.assembly]::load(“Microsoft.Sharepoint, version=12.0.0.0, culture=neutral, publickeytoken=71e9bce111e9429c”)

# The import and export functionality is part of the Microsoft.SharePoint.Deployment namespace

[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Deployment”)

# Open a new site object, this requires a dispose

$site = new-object Microsoft.SharePoint.SPSite($siteURL)

# Open a new web

$web = $site.OpenWeb($webName)
 

# Create a new SPExportObject
$exportObject = new-object Microsoft.SharePoint.Deployment.SPExportObject

# Populate the SPExportObject properties

$exportObject.Id = $web.ID;

$exportObject.IncludeDescendants = [Microsoft.SharePoint.Deployment.SPIncludeDescendants]::All

$exportObject.Type = [Microsoft.SharePoint.Deployment.SPDeploymentObjectType]::Web

# Create a new object that holds all of the settings

$settings = new-object Microsoft.SharePoint.Deployment.SPExportSettings

$settings.SiteUrl = $siteURL

$settings.ExportMethod = [Microsoft.SharePoint.Deployment.SPExportMethodType]::ExportAll

$settings.FileLocation = $fileLocation

$settings.FileCompression = $true

$settings.BaseFileName = $fileName

$settings.ExcludeDependencies = $false

$settings.ExportObjects.Add($exportObject)

$settings.OverwriteExistingDataFile = $true

$settings.IncludeVersions = [Microsoft.SharePoint.Deployment.SPIncludeVersions]::All

# Create a new SPExport object based off the the settings of the SPExportSettings object

$export = new-object Microsoft.SharePoint.Deployment.SPExport($settings)

# Start the export

$export.Run() 

# Dispose of the site object to release it from memory

$site.Dispose()

The Import Script

# Specify the properties of the web site that will import the file

$siteURL = http://ps-moss:11030/”

# This is the parent web of where the file will be imported.

$webName = “test2”

$webURL = $siteURL + $webName

$fileName = “exportedWeb.cmp”

$fileLocation = “c:export”

# Load the Microsoft.SharePoint DLL

[void][system.reflection.assembly]::load(“Microsoft.Sharepoint, version=12.0.0.0, culture=neutral, publickeytoken=71e9bce111e9429c”)

# The import and export functionality is part of the Microsoft.SharePoint.Deployment namespace

[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Deployment”)

# Create a new object that holds all of the settings

$settings = new-object Microsoft.SharePoint.Deployment.SPImportSettings

$settings.SiteUrl = $siteURL

$settings.WebUrl = $webURL

$settings.FileLocation = $fileLocation

$settings.FileCompression = $true

$settings.RetainObjectIdentity = $true

$settings.UserInfoDateTime = [Microsoft.SharePoint.Deployment.SPImportUserInfoDateTimeOption]::ImportAll

$settings.BaseFileName = $fileName

$import = new-object Microsoft.SharePoint.Deployment.SPImport($settings)

$import.Run()

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