Moving a SharePoint Document library to a new SiteCollection via PowerShell

Case

Recently I ran into a typical SharePoint problem. A SharePoint 2010 site exploded in storage usage. Users was uploading like crazy to one single document library and I needed to move the document library and a few lists to a new site on a new SiteCollection. The source site, was build on a SiteTemplate, that the client no longer had access to. That meant, that a traditional backup/restore didn’t work. I needed to export/import the single Document library and lists one-by-one.

According to Microsoft Best Practise (Which you almost/sometimes always should follow) you have to keep the size of your Content Database below the 100 GB mark. Some say 200 GB but the Health Analyzer is acting out when you reach 100 GB. This problem is thankfully not that big a problem if you are using 2013. The newer SharePoint and SQL servers are better at handling larger DB-sizes, but never the less I like to keep my DB’s no larger than 200 GB.

Solution

There are two ways of making the move. One is to make a backup of your entire site and then restore the backup on a new SiteCollection. That didn’t work for me as I needed only to move a few document libraries and did not want the dependencies to follow. So I went with plan B. Moving the lists and libraries individually.

  1. Create a new Content Database for the WebApplication
  2. Set the current Content Database to “Offline”
  3. Create a new SiteCollection and a new site, that you wish to use as the destination site (That should place the new SiteCollection on the new Content Database)
  4. Use the script below to copy the lists and libraries to the new SiteCollection, that is housed on your new Content Database

PowerShell Script

##Don't forget to tweak the code below to fit your needs.
add-pssnapin microsoft.sharepoint.powershell

##Location of temporary backup files
$path = "e:\"

## Add a few variables to work with
$OldURL = "http://intranet.domain.com/departement/"
$NewURL = "http://intranet.domain.com/departements/"
$departement = "sales"

##All lists that needs to be exported
$list = @("Clients","Contacts")

##All libraries that needs to be exported
md -Path ($path + "\" + $afdeling)

##Export and import the lists
foreach($i in $list){Export-SPWeb ($OldURL + $departement) -Path ($path + $departement + "\" + $i + ".cmp") -ItemUrl ("/departement/" + $departement + "/" + $i) -IncludeVersions All -IncludeUserSecurity}

foreach($i in $list){Import-SPWeb ($NewURL + $departement) -Path ($path + $departement + "\" + $i + ".cmp") –UpdateVersions Overwrite -IncludeUserSecurity}

##Export and import the libraries
foreach($i in $list){Export-SPWeb ($OldURL + $departement) -Path ($path + $departement + "\" + $i + ".cmp") -ItemUrl ("/departement/" + $departement + "/" + $i) -IncludeVersions All -IncludeUserSecurity}

foreach($i in $list){Import-SPWeb ($NewURL + $departement) -Path ($path + $departement + "\" + $i + ".cmp") –UpdateVersions Overwrite -IncludeUserSecurity}

##Cleanup
Remove-Item -Recurse -Force ($path + $afdeling)

Details

In SharePoint you have several ways to scale out your content. Depending on your client and their needs you can make some estimates on how much storage they will take up. But in my experience, your client will often surprise you and use their SharePoint in a way you didn’t expect.

Architecture of SharePoint:

  • WebApplication
    • SiteCollections
      • Sites
        • Lists and libraries

Rules of SharePoint:

  • You can have lots of Content Databases in one WebApplication.
    • You can have lots of SiteCollections in one Content Database
      • You can have lots of Sites in one SiteCollection
        • You can have lots of Lists and libraries in one site
  • A SiteCollection can only be housed in one Content Database
  • SiteCollections can easily be moved around between Content Databases with a simple PowerShell command
  • Sites have no PowerShell command for moving Sites around between SiteCollections
  • In PowerShell a SharePoint Site is called SPWeb (which is just darn confusing)